Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 如何按汇总的组进行排序?_Sorting_Sql Server 2008 R2_Grouping - Fatal编程技术网

Sorting 如何按汇总的组进行排序?

Sorting 如何按汇总的组进行排序?,sorting,sql-server-2008-r2,grouping,Sorting,Sql Server 2008 R2,Grouping,假设我在SQLServer2008R2中有一个名为Purchase的表,有两列:Purchaser和Expense 假设该表有以下行: Purchaser Expenditure --------- ----------- Alex 200 Alex 300 Alex 500 Bob 300 Bob 400 Charlie 200 Charlie 600 Derek

假设我在SQLServer2008R2中有一个名为Purchase的表,有两列:Purchaser和Expense

假设该表有以下行:

Purchaser    Expenditure
---------    -----------
Alex         200
Alex         300
Alex         500
Bob          300
Bob          400
Charlie      200
Charlie      600
Derek        100
Derek        300
现在我有一个问题:

从采购组中按采购商选择采购商、支出、汇总支出作为汇总支出

这将返回以下内容:

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
--------------------------------
换句话说,我正在对组进行排序,按升序使用组行中的400、700、800和1000

有人能建议什么查询会返回这个结果集吗

;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;