日期:2014-05-18 浏览次数:20582 次
select isnull(name,'小计') name,sum(value) value from tb group by name with rollup
------解决方案--------------------
select name,sum(value) as values from tb group by name union all select '小计',sum(value) as values from tb
------解决方案--------------------
select isnull(Name,'小计') as Name,sum(value) as value from 表 group by Name with rollup
------解决方案--------------------
select name,sum(value) value from tb group by name union all select '小计' name,sum(value) value from tb
------解决方案--------------------
declare @t table(ID int,Name varchar(10),value numeric(4,1)) insert into @t values(1,'A',1.0 ) insert into @t values(2,'A',2.0 ) insert into @t values(3,'B',15.0) insert into @t values(4,'B',12.0) insert into @t values(5,'A',25.0) select isnull(Name,'小计') as Name,sum(value) as value from @t group by Name with rollup /* Name value ---------- ---------------------------------------- A 28.0 B 27.0 小计 55.0 */
------解决方案--------------------
create table tb(ID int, Name varchar(101),value numeric(10,1)) insert into tb select 1, 'A', 1.0 union all select 2, 'A', 2.0 union all select 3, 'B', 15.0 union all select 4, 'B', 12.0 union all select 5, 'A', 25.0 select case when (grouping(name)=1) then '合计' else isnull(name,'unknow') end as name,sum(value) as value from tb group by name with cube /* name value ----------------- A 28.0 B 27.0 合计 55.0 */ drop table tb