4
    SELECT region, person, sum(dollars) as thousands
    FROM sales
    GROUP BY region, person
    ORDER BY region, sum(dollars) desc

上面的 SQL 生成每个区域的销售人员的完整列表,如下所示

    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
4

7 回答 7

1

我做了类似burnall建议的事情。我对“top 1 with ties”部分不太喜欢,所以我把整个事情变成了一个子查询,并选择了ranking = 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

请注意,这也适用于平局,因为 rank() 似乎对相等的总和进行了相同的排名。

于 2010-02-20T15:20:39.650 回答
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
于 2010-02-19T07:22:18.567 回答
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
于 2010-02-19T07:31:19.353 回答
0

在 SQL Server 2005 及更高版本ROW_NUMBER中使用PARTITION 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
于 2010-02-19T07:31:37.310 回答
0

首先我不明白为什么 count(*) 在 $. 我的解决方案类似于现有的,但更短,我相信更快

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
于 2010-02-19T07:39:19.287 回答
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
于 2010-02-20T16:22:57.450 回答
0

查找每个地区销售额排名前 5 的销售人员

select *
from
(
     select region, 
            [Customer Name],
            rank() over(partition by region order by sum(sales) desc) as ranking
     from Orders 
     group by region, [Customer Name] 
) temp
where ranking between 1 and 5
于 2019-09-19T07:18:07.133 回答