联机帮助中的例子
--------------------------------------
用 ROLLUP 汇总数据
在生成包含小计和合计的报表时,ROLLUP 运算符很有用。ROLLUP 运算符生成的结果集类似于 CUBE 运算符所生成的结果集。有关更多信息,请参见用 CUBE 汇总数据。 CUBE 和 ROLLUP 之间的区别在于: CUBE 生成的结果集显示了所选列中值的所有组合的聚合。
ROLLUP 生成的结果集显示了所选列中值的某一层次结构的聚合。
例如,简单表 Inventory 中包含:Item Color Quantity
-------------------- -------------------- --------------------------
Table Blue 124
Table Red 223
Chair Blue 101
Chair Red 210 下列查询将生成小计报表:SELECT CASE WHEN (GROUPING(Item) = 1) THEN 'ALL'
ELSE ISNULL(Item, 'UNKNOWN')
END AS Item,
CASE WHEN (GROUPING(Color) = 1) THEN 'ALL'
ELSE ISNULL(Color, 'UNKNOWN')
END AS Color,
SUM(Quantity) AS QtySum
FROM Inventory
GROUP BY Item, Color WITH ROLLUPItem Color QtySum
-------------------- -------------------- --------------------------
Chair Blue 101.00
Chair Red 210.00
Chair ALL 311.00
Table Blue 124.00
Table Red 223.00
Table ALL 347.00
ALL ALL 658.00 (7 row(s) affected)如果查询中的 ROLLUP 关键字更改为 CUBE,那么 CUBE 结果集与上述结果相同,只是在结果集的末尾还会返回下列两行:ALL Blue 225.00
ALL Red 433.00 CUBE 操作为 Item 和 Color 中值的可能组合生成行。例如,CUBE 不仅报告与 Item 值 Chair 相组合的 Color 值的所有可能组合(Red、Blue 和 Red + Blue),而且报告与 Color 值 Red 相组合的 Item 值的所有可能组合(Chair、Table 和 Chair + Table)。对于 GROUP BY 子句中右边的列中的每个值,ROLLUP 操作并不报告左边一列(或左边各列)中值的所有可能组合。例如,ROLLUP 并不对每个 Color 值报告 Item 值的所有可能组合。ROLLUP 操作的结果集具有类似于 COMPUTE BY 所返回结果集的功能;然而,ROLLUP 具有下列优点: ROLLUP 返回单个结果集;COMPUTE BY 返回多个结果集,而多个结果集会增加应用程序代码的复杂性。
ROLLUP 可以在服务器游标中使用;COMPUTE BY 不可以。
有时,查询优化器为 ROLLUP 生成的执行计划比为 COMPUTE BY 生成的更为高效。
--------------------------------------
用 ROLLUP 汇总数据
在生成包含小计和合计的报表时,ROLLUP 运算符很有用。ROLLUP 运算符生成的结果集类似于 CUBE 运算符所生成的结果集。有关更多信息,请参见用 CUBE 汇总数据。 CUBE 和 ROLLUP 之间的区别在于: CUBE 生成的结果集显示了所选列中值的所有组合的聚合。
ROLLUP 生成的结果集显示了所选列中值的某一层次结构的聚合。
例如,简单表 Inventory 中包含:Item Color Quantity
-------------------- -------------------- --------------------------
Table Blue 124
Table Red 223
Chair Blue 101
Chair Red 210 下列查询将生成小计报表:SELECT CASE WHEN (GROUPING(Item) = 1) THEN 'ALL'
ELSE ISNULL(Item, 'UNKNOWN')
END AS Item,
CASE WHEN (GROUPING(Color) = 1) THEN 'ALL'
ELSE ISNULL(Color, 'UNKNOWN')
END AS Color,
SUM(Quantity) AS QtySum
FROM Inventory
GROUP BY Item, Color WITH ROLLUPItem Color QtySum
-------------------- -------------------- --------------------------
Chair Blue 101.00
Chair Red 210.00
Chair ALL 311.00
Table Blue 124.00
Table Red 223.00
Table ALL 347.00
ALL ALL 658.00 (7 row(s) affected)如果查询中的 ROLLUP 关键字更改为 CUBE,那么 CUBE 结果集与上述结果相同,只是在结果集的末尾还会返回下列两行:ALL Blue 225.00
ALL Red 433.00 CUBE 操作为 Item 和 Color 中值的可能组合生成行。例如,CUBE 不仅报告与 Item 值 Chair 相组合的 Color 值的所有可能组合(Red、Blue 和 Red + Blue),而且报告与 Color 值 Red 相组合的 Item 值的所有可能组合(Chair、Table 和 Chair + Table)。对于 GROUP BY 子句中右边的列中的每个值,ROLLUP 操作并不报告左边一列(或左边各列)中值的所有可能组合。例如,ROLLUP 并不对每个 Color 值报告 Item 值的所有可能组合。ROLLUP 操作的结果集具有类似于 COMPUTE BY 所返回结果集的功能;然而,ROLLUP 具有下列优点: ROLLUP 返回单个结果集;COMPUTE BY 返回多个结果集,而多个结果集会增加应用程序代码的复杂性。
ROLLUP 可以在服务器游标中使用;COMPUTE BY 不可以。
有时,查询优化器为 ROLLUP 生成的执行计划比为 COMPUTE BY 生成的更为高效。
(select f1 as a1,f1,f2 from table1
union all select f1+'小计','小计',sum(f2) from table1 group by f1
union all select '总计','总计',sum(f2) from table1) a
order by f1
create table table1(f1 char(3),f2 int)
insert table1 select '001',10
union all select '001',20
union all select '001',30
union all select '002',20
union all select '002',30
union all select '002',40
union all select '003',50
union all select '004',60
union all select '004',70
go--查询统计
select f1,f2
from(
select f1,f2,s0=0,s1=f1,s2=0 from table1
union all
select f1=case grouping(f1) when 1 then '总计' else f1+'小计' end,
f2=sum(f2),s0=grouping(f1),s1=f1,s2=1
from table1 group by f1 with rollup
)a order by s0,s1,s2
go--删除测试
drop table table1/*--结果
f1 f2
------- -----------
001 10
001 20
001 30
001小计 60
002 20
002 30
002 40
002小计 90
003 50
003小计 50
004 60
004 70
004小计 130
总计 330(所影响的行数为 14 行)
--*/
Create table table1
(f1 Varchar(10),
f2 Int)
--插入数据
Insert Table1 Values('001', 10)
Insert Table1 Values('001', 20)
Insert Table1 Values('001', 30)
Insert Table1 Values('002', 20)
Insert Table1 Values('002', 30)
Insert Table1 Values('002', 40)
Insert Table1 Values('003', 50)
Insert Table1 Values('004', 60)
Insert Table1 Values('004', 70)
--测试
Select * from table1
Union All
Select f1+N'小计' As f1 ,SUM(f2) As f2 from table1 Group By f1
Union All
Select N'总计' As f1,SUM(f2) As f2 from table1
Order by f1,f2
--删除测试环境
Drop table table1
--结果
/*
f1 f2
001 10
001 20
001 30
001小计 60
002 20
002 30
002 40
002小计 90
003 50
003小计 50
004 60
004 70
004小计 130
总计 330
*/
insert into @table select '2',3 union all select '2',4 union all select '2',5
insert into @table select '3',3 union all select '3',4 union all select '3',5
select id=case when grp=1 and id is not null then '小计' when grp=1 and id is null then '合计' else id end,c
from(
select id,sum(c) as c,grouping(c) as 'grp'
from @table
group by id,c
with cube
)t
where (id is not null or grp=1)
create table table1(f1 char(3),f2 int)
insert table1 select '001',10
union all select '001',20
union all select '001',30
union all select '002',20
union all select '002',30
union all select '002',40
union all select '003',50
union all select '004',60
union all select '004',70
go--如果f1+f2不重复,也可以使用
select f1=case
when grouping(f1)=1 then '总计'
when grouping(f2)=1 then '小计'
else f1 end,
f2=sum(f2)
from table1
group by f1,f2 with rollup
go--删除测试
drop table table1/*--结果f1 f2
---- -----------
001 10
001 20
001 30
小计 60
002 20
002 30
002 40
小计 90
003 50
小计 50
004 60
004 70
小计 130
总计 330(所影响的行数为 14 行)
--*/
f1 f2
001 10
001 20
001 30
002 20
002 30
002 40
003 50
004 60
004 70
小计 60
小计 90
小计 50
小计 130
总计 330 但是我的要求是这样的:
f1 f2
001 10
001 20
001 30
小计 60002 20
002 30
002 40
小计 90003 50
小计 50004 60
004 70
小计 130总计 330 谢谢!
请问 zjcxc(邹建) ( ) 信誉:558 大哥
select f1=case grouping(f1) when 1 then '总计' else f1+'小计' end,
f2=sum(f2),s0=grouping(f1),s1=f1,s2=1
from table1 group by f1 with rollup您这里的 when 1 表示什么? 我看不明白啊
select f1,f2 from
(select f1 as a1,f1,f2 from table1
union all select f1+'小计','小计',sum(f2) from table1 group by f1
union all select '总计','总计',sum(f2) from table1) a
order by a1