假设我在 SQL Server 2008 R2 中有一个Purchase
用两列调用的表:Purchaser
和 Expenditure
.
假设该表具有以下行:
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
--------------------------------
换句话说,我正在对组进行排序,使用,400
和在组行中按升序排列。700
800
1000
谁能建议什么查询会返回这个结果集?