读书人

请问:关于 with rollup 的有关问题

发布时间: 2012-01-21 21:31:43 作者: rapoo

请教:关于 with rollup 的问题
select a1=isnull(a1, '合计 '),a2,count(*)
from table
group by a1,a2 with rollup

执行上面语句,最后一行有 "合计 ",而执行下面语句,却没有 "合计 ":

select a1=isnull(a1+ '- '+a2), '合计 '),count(*)
from table
group by isnull(a1+ '- '+a2), '合计 ') with rollup

请问第2条语句该怎样写?

[解决办法]
select a1=case when grouping(a1+ '- '+a2)=1 then '合计 ' end,count(*)
from table
group by (a1+ '- '+a2) with rollup

[解决办法]
select a1=isnull(x, '合计 '),count(*)
from (
select a1+ '- '+a2 as x
from table
) as t
group by x with rollup

读书人网 >SQL Server

热点推荐