在SQL查询中将别名命名为汇总?

时间:2013-09-19 14:26:19

标签: sql tsql stored-procedures sql-server-2008-r2 rollup

我在我的SQL查询中使用with rollup。我没有获得汇总的别名。

我的SQL是

SELECT [Column1],
       sum([Column2])
FROM   Tablea
GROUP  BY [Column2] WITH ROLLUP 

返回

s       8
t       8
j       8
null    24 

如何替换总行中的NULL

3 个答案:

答案 0 :(得分:18)

您可以在CASE表达式中使用GROUPING函数。

SELECT CASE
         WHEN GROUPING([Column1]) = 1 THEN 'Total'
         ELSE [Column1]
       END [Column1],
       sum([Column2])
FROM   Tablea
GROUP  BY [Column1] WITH ROLLUP 

SQL Fiddle

答案 1 :(得分:1)

select 
isnull([column1],'rollup'), 
sum([column2] )
from tableA
group by [column1] 
WITH ROLLUP

答案 2 :(得分:-4)

SELECT ifnull([column1],'total'),
       sum([Column2])
FROM   Tablea
GROUP  BY [Column2] WITH ROLLUP