注册 登录
编程论坛 SQL Server论坛

[求助]对数据进行分类求和及求其总和

小水滴 发布于 2006-10-28 10:15, 1327 次点击

--创建表:
create table invertery (item char(10),color varchar(10),quantity int)
insert invertery
select 'Chair','Blue',50 union all
select 'Chair','Red',100 union all
select 'Table','Blue',24 union all
select 'Table','Red',123 union all
select 'Chair','Blue',51 union all
select 'Chair','Red',110 union all
select 'Table','Blue',100 union all
select 'Table','Red',100

--对数据进行分类求和及求其总和查询:
select item,color,sum(quantity) as qtysum
from invertery
group by item,color
with rollup

查询结果为:
item color qtysum
------------ --------------- ---------------
Chair Blue 101
Chair Red 210
Chair NULL 311
Table Blue 124
Table Red 223
Table NULL 347
NULL NULL 658

现在想得到以下的查询结果(多了红色的字), 查询语句该如何写?
查询结果为:
item color qtysum
------------ --------------- ---------------
Chair Blue 101
Chair Red 210
Chair 汇总 NULL 311
Table Blue 124
Table Red 223
Table 汇部 NULL 347
总计 NULL 658




5 回复
#2
李彬2006-10-28 11:42

去掉with rollup就可以了!

#3
小水滴2006-10-28 12:26
不对吧, 去掉了with rollup,分类总和都没有了
#4
LouisXIV2006-10-28 12:32

加个Case语句即可

#5
小水滴2006-10-29 21:06
谢谢版主的提示!
#6
棉花糖ONE2007-03-01 23:07
case对grouping(列名)进行判断就能实现
1