Alias names to with rollup in SQL queries?

為{幸葍}努か 提交于 2020-01-09 11:05:28

问题


I am using with rollup in my sql query. I am not getting alias name for rollup.

My SQL is

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

Which returns

s       8
t       8
j       8
null    24 

How can I replace the NULL in the total row?


回答1:


You can use the GROUPING function in a CASE expression.

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

SQL Fiddle




回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/18897415/alias-names-to-with-rollup-in-sql-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!