【发布时间】:2013-11-01 22:38:57
【问题描述】:
在 SQL Server 中使用ROLLUP 时,如何获取明细行上方的小计行?
这是你使用ROLLUP时通常会得到的:
Group Name Total Sales
---------------- --------------- ------------
Europe France 74569.00
Europe Germany 59456.00
Europe United Kingdom 78327.00
Europe NULL 212352.00 << sub total row for Europe appears **after** all the individual rows for Europe.
North America Northwest 208774.00
North America Southeast 145477.00
North America Southwest 164232.00
North America NULL 518483.00
Pacific Australia 93403.00
Pacific NULL 93403.00
这是预期的结果集:
Group Name Total Sales
---------------- --------------- ------------
Europe NULL 212352.00 << sub total row for Europe needs to appear **before** the individual rows for Europe.
Europe France 74569.00
Europe Germany 59456.00
Europe United Kingdom 78327.00
North America NULL 518483.00
North America Northwest 208774.00
North America Southeast 145477.00
North America Southwest 164232.00
Pacific NULL 93403.00
Pacific Australia 93403.00
使用的查询:
SELECT [Group], [Name], SUM([SalesYTD]) AS 'Total Sales'
FROM #TempTable
GROUP BY [Group], [Name] WITH ROLLUP
有什么想法可以得到这个输出吗?
【问题讨论】:
-
。 .您可以发布您用来获取第一组结果的查询吗?
-
请在“明细行上方的小计行”中明确..??
-
@GordonLinoff:我将其添加到问题中。
-
@ShivanshuSrivastava:我指的是使用
ROLLUP时出现在结果集中的小计行。我还在问题中添加了指示我所指行的文本。谢了。
标签: sql sql-server rollup