2010-02-19 70 views
4
SELECT region, person, sum(dollars) as thousands 
    FROM sales 
    GROUP BY region, person 
    ORDER BY region, sum(dollars) desc 

的SQL上面產生的銷售人員的完整列表,每個區域這樣SQL服務器 - 頂Saleperson每個地區

region person  thousands 

    canada mike smith $114 
    canada joe blog $76 
    canada pete dodd $45 
    usa john doe $253 
    usa jane smyth $120 
    europe pieter tsu $546 
    europ mike lee $520 

如果我只顯示每個區域的頂部銷售人員感興趣的(如下),我怎麼能最好地做到這一點?

region person  thousands 

    canada mike smith $114 
    usa john doe $253 
    europe pieter tsu $546 
+0

您正在使用哪個版本的Sql Server? – 2010-02-19 07:21:24

+1

邊界案例:如果有兩個在同一地區表現最佳的銷售人員,您希望展示什麼?只是任何?都?要麼? – van 2010-02-19 07:38:25

+2

你爲什麼使用計數?伯爵只會給出匹配的記錄數量,而不是銷售額的總和。除非每次銷售正好是$ 1000 ... 如果您想要總和,請使用總和... – jle 2010-02-19 07:47:18

回答

1

我已經做了一些類似burnall建議。我沒有得到太多的愛與「頂1的關係」的一部分,所以我做了整個事情的子查詢並選擇行,其中排名= 1

select * 
from 
(
    select region, 
      person, 
      rank() over(partition by region order by sum(dollars) desc) as ranking 
    from sales 
    group by region, 
       person 

) temp 
where ranking = 1 

注意,這也適用於因爲排名的關係( )似乎在相同的總和上排名相同。

0

您可以使用max()聚合。它可能比其他替代效率較低,因爲你會出現兩次

SELECT region,person,max(thousands) FROM 
(SELECT region, person, count(*) as thousands 
FROM sales 
GROUP BY region, person) tmp 
GROUP BY region, person 
ORDER BY region, max(thousands) desc 
+0

你試過這個嗎? **不能在包含聚集或子查詢的表達式上執行聚合函數。** – 2010-02-19 07:26:44

+0

不,我沒有。我現在寫的另一種選擇,我也沒有嘗試,只是記憶。這通常會導致我失敗:) – 2010-02-19 07:51:24

0

做組使用SQL Server 2005 +,你可以做到這一點使用一個ROW_NUMBER()

看一看這個完整的例子。

DECLARE @sales TABLE(
     region VARCHAR(50), 
     person VARCHAR(50), 
     Sales FLOAT 
) 



INSERT INTO @sales SELECT 'canada','mike smith',1 
INSERT INTO @sales SELECT 'canada','mike smith',1 
INSERT INTO @sales SELECT 'canada','mike smith',1 
INSERT INTO @sales SELECT 'canada','mike smith',1 

INSERT INTO @sales SELECT 'canada','joe blog',1 
INSERT INTO @sales SELECT 'canada','joe blog',1 

INSERT INTO @sales SELECT 'canada','pete dodd',1 


INSERT INTO @sales SELECT 'usa','john doe',1 
INSERT INTO @sales SELECT 'usa','john doe',1 

INSERT INTO @sales SELECT 'usa','jane smyth',1 

INSERT INTO @sales SELECT 'europe','pieter tsu',1 
INSERT INTO @sales SELECT 'europe','pieter tsu',1 

INSERT INTO @sales SELECT 'europe','mike lee',1 

;WITH Counts AS(
     SELECT region, 
       person, 
       count(*) as thousands 
     FROM @sales 
     GROUP BY region, 
        person 
), CountVals AS(
     SELECT *, 
       ROW_NUMBER() OVER(PARTITION BY region ORDER BY thousands DESC) ROWID 
     FROM  Counts 
) 
SELECT * 
FROM CountVals 
WHERE ROWID = 1 
0

在SQL Server 2005及更高版本中,使用ROW_NUMBERPARTITION BY。以下應該工作(未測試,可能可以縮短):

WITH total_sales 
AS (SELECT  region, person, count(*) as thousands 
    FROM  sales 
    GROUP BY region, person 
    ORDER BY region, count(*) desc 
) 
, ranked_sales 
AS (SELECT  region, person, thousands, 
       ROW_NUMBER() OVER (PARTITION BY region ORDER BY thousands DESC, person) AS region_rank 
    FROM  total_sales 
) 
SELECT region, person, thousands 
FROM ranked_sales 
WHERE region_rank = 1 
0

首先,我不明白爲什麼計數(*)是在$。 我的解決方案是類似於現有的,但更短,我相信快

select top 1 with ties region, person, rank() over(partition by region order by count(*) desc) 
from sales 
group by region, person 
order by 3 
+0

謝謝!我跟伯爵放屁。更改爲總和:) – user246211 2010-02-20 12:59:06

+0

我沒有太多的運氣與「領帶1」部分,所以我擺脫它,並作爲一個子查詢運行,只選擇排名1.感謝指出我在正確的方向! – user246211 2010-02-20 15:25:20

+0

帶領帶的頂部1有什麼問題?我已經通過一個簡單的測試進行了檢查。顯示你的查詢,也許我們可以改進它甚至更多) – burnall 2010-02-20 22:07:41

0

這並不難。這個查詢將完成你想要的。

select distinct region, 
     (select top 1 person 
     from Sales s2 where s2.region = s1.region 
     group by person 
     order by SUM(dollars) desc) as person, 
      (select top 1 SUM(dollars) 
     from Sales s2 where s2.region = s1.region 
     group by person 
     order by SUM(dollars) desc) as thousands 
     from sales s1