日期:2014-05-19  浏览次数:20529 次

用Group后的合计
原来是这样的
select   count(a)   as   aa,b   from   #   group   by   b
现在要添加一行,aa的合计

------解决方案--------------------
select count(a) as aa,count(1) as '合计 ',b from # group by b

------解决方案--------------------
select count(aa) from (select count(a) as aa,b from # group by b) t group by aa
------解决方案--------------------
try

select count(a) as aa,b from # group by b with rollup
------解决方案--------------------
Create Table TEST
(b Int,
a DateTime)
Insert TEST Select 1, '2007-01-03 '
Union All Select 1, '2007-01-08 '
Union All Select 2, '2007-01-06 '
Union All Select 3, '2007-01-03 '
Union All Select 3, '2007-02-03 '
Union All Select 3, '2007-03-03 '
GO
Select COUNT(a) As aa, b From TEST Group By b With Rollup
GO
Drop Table TEST
--Result
/*
aa b
2 1
1 2
3 3
6 NULL
*/
------解决方案--------------------
select isnull(b, '合计 ') as b , count(a) as aa from # group by b with rollup
------解决方案--------------------
我和梅一样,