【问题标题】:How do I sort by a rolled up group?如何按汇总组排序?
【发布时间】:2012-05-04 11:21:31
【问题描述】:

假设我在 SQL Server 2008 R2 中有一个名为 Purchase 的表,其中包含两列:PurchaserExpenditure

假设表格有以下几行:

Purchaser    Expenditure
---------    -----------
Alex         200
Alex         300
Alex         500
Bob          300
Bob          400
Charlie      200
Charlie      600
Derek        100
Derek        300

现在我有这个查询:

SELECT Purchaser, Expenditure, SUM(Expenditure) AS SumExpenditure FROM Purchase GROUP BY Purchaser, Expenditure WITH ROLLUP

这将返回以下内容:

Purchaser    Expenditure    SumExpenditure
---------    -----------    --------------
Alex         200            200
Alex         300            300
Alex         500            500
--------------------------------
Alex         NULL           1000
--------------------------------
Bob          300            300
Bob          400            400
--------------------------------
Bob          NULL           700
--------------------------------
Charlie      200            200
Charlie      600            600
--------------------------------
Charlie      NULL           800
--------------------------------
Derek        100            100
Derek        300            300
--------------------------------
Derek        NULL           400
--------------------------------

(添加线条以强调汇总的金额。)

我希望能够按分组数量对组进行排序,以便最终得到如下结果集:

Purchaser    Expenditure    SumExpenditure
---------    -----------    --------------
Derek        100            100
Derek        300            300
--------------------------------
Derek        NULL           400
--------------------------------
Bob          300            300
Bob          400            400
--------------------------------
Bob          NULL           700
--------------------------------
Charlie      200            200
Charlie      600            600
--------------------------------
Charlie      NULL           800
--------------------------------
Alex         200            200
Alex         300            300
Alex         500            500
--------------------------------
Alex         NULL           1000
--------------------------------

换句话说,我正在对组进行排序,在组行中按升序使用4007008001000

谁能建议什么查询会返回这个结果集?

【问题讨论】:

    标签: sorting sql-server-2008-r2 grouping


    【解决方案1】:
    ;WITH x AS 
    (
        SELECT Purchaser, Expenditure, s = SUM(Expenditure) 
        FROM dbo.Purchase 
        GROUP BY Purchaser, Expenditure WITH ROLLUP
    ),
    y AS 
    (
        SELECT Purchaser, s FROM x 
        WHERE Expenditure IS NULL
        AND Purchaser IS NOT NULL
    ),
    z AS 
    (
        SELECT Purchaser, s, rn = ROW_NUMBER() OVER (ORDER BY s)
        FROM y
    )
    SELECT x.Purchaser, x.Expenditure, x.s FROM x 
    INNER JOIN z ON x.Purchaser = z.Purchaser
    ORDER BY z.rn, CASE WHEN z.s IS NULL THEN 2 ELSE 1 END;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-24
      • 2016-07-31
      • 2018-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      相关资源
      最近更新 更多