t-sql按类别分组并获得前n个值

时间:2011-02-23 13:49:58

标签: tsql

想象一下,我有这张桌子:

Month | Person | Value
----------------------
Jan   | P1     | 1
Jan   | P2     | 2
Jan   | P3     | 3
Feb   | P1     | 5
Feb   | P2     | 4
Feb   | P3     | 3
Feb   | P4     | 2
...

如何构建t-sql查询以获取前2个值行,使用其他值之和构建第3个?

这样的事情:

RESULT: 
Month | Person | Value
----------------------
Jan   | P3     | 3
Jan   | P2     | 2
Jan   | Others | 1 -(sum of the bottom value - in this case (Jan, P1, 1))
Feb   | P1     | 5
Feb   | P2     | 4
Feb   | Others | 5 -(sum of the bottom values - in this case (Feb, P3, 3) and (Feb, P4, 2))

由于

3 个答案:

答案 0 :(得分:5)

假设您使用的是SQL Server 2005或更高版本,使用CTE就可以了。

  • ROW_NUMBER附加到每一行,从最高值开始,每月重置一次。
  • SELECT此查询中每月的前2行(rownumber< = 2)
  • UNION剩下的行(rownumber> 2)

SQL声明

;WITH Months (Month, Person, Value) AS (
  SELECT 'Jan', 'P1', 1 UNION ALL
  SELECT 'Jan', 'P2', 2 UNION ALL
  SELECT 'Jan', 'P3', 3 UNION ALL
  SELECT 'Feb', 'P1', 5 UNION ALL
  SELECT 'Feb', 'P2', 4 UNION ALL
  SELECT 'Feb', 'P3', 3 UNION ALL
  SELECT 'Feb', 'P4', 2
),
q AS (
  SELECT  Month
          , Person
          , Value
          , RowNumber = ROW_NUMBER() OVER (PARTITION BY Month ORDER BY Value DESC)
  FROM    Months
)
SELECT  Month
        , Person
        , Value
FROM    (        
          SELECT  Month
                  , Person
                  , Value
                  , RowNumber
          FROM    q
          WHERE   RowNumber <= 2 
          UNION ALL
          SELECT  Month
                  , Person = 'Others'
                  , SUM(Value)
                  , MAX(RowNumber)
          FROM    q
          WHERE   RowNumber > 2        
          GROUP BY
                  Month 
        ) q                          
ORDER BY
        Month DESC
        , RowNumber

Kudo去Andriy教我一些新技巧。

答案 1 :(得分:3)

;WITH atable (Month, Person, Value) AS (
  SELECT 'Jan', 'P1', 1 UNION ALL
  SELECT 'Jan', 'P2', 2 UNION ALL
  SELECT 'Jan', 'P3', 3 UNION ALL
  SELECT 'Feb', 'P1', 5 UNION ALL
  SELECT 'Feb', 'P2', 4 UNION ALL
  SELECT 'Feb', 'P3', 3 UNION ALL
  SELECT 'Feb', 'P4', 2
),
numbered AS (
  SELECT
    Month, Person, Value,
    rownum = ROW_NUMBER() OVER (PARTITION BY Month ORDER BY Value DESC)
  FROM atable
),
grouped AS (
  SELECT
    Month, Person, Value,
    Grp = CASE WHEN rownum < 3 THEN rownum ELSE 3 END
  FROM numbered
)
SELECT
  Month,
  Person = CASE Grp WHEN 3 THEN 'Others' ELSE MAX(Person) END,
  Value = SUM(Value)
FROM grouped
GROUP BY Month, Grp
ORDER BY Month DESC, Grp

答案 2 :(得分:2)

WITH NTable AS
(
    SELECT [Month],
                   Person,
                   Value,
                   ROW_NUMBER() OVER (PARTITION BY [Month] ORDER BY Value DESC) 
                       AS Rownumber 
            FROM MyTable
)
SELECT  t.[Month], 
        CASE Rownumber WHEN 1 THEN t.Person WHEN 2 THEN t.Person ELSE 'Others' END As Person, 
        SUM(t.Value) As [Sum] 
FROM NTable t
GROUP BY t.[Month], CASE Rownumber WHEN 1 THEN t.Person WHEN 2 THEN t.Person ELSE 'Others' END
ORDER BY t.[Month]