管理杂谈OA答疑ERP答疑教程搜索

统计报表(用ROLLUP 汇总数据)




作者 : hongyuan
标题 : 统计报表(用rollup 汇总数据)
关键字:
分类 : sql server 2000
密级 : 私有


统计报表(用rollup 汇总数据)
原贴:http://community.csdn.net/expert/topic/4313/4313978.xml?temp=.691601


表inventory
item color quantity
-------------------- -------------------- --------------------------
table blue 124
table red 223
chair blue 101
chair red 210

要得到下面结果:

item color qtysum
-------------------- -------------------- --------------------------
chair blue 101.00
chair red 210.00
chair小计 311.00
table blue 124.00
table red 223.00
table小计 347.00
总计 658.00


---该问题是一个典型的使用rollup生成结合的例子,联机帮助也有相关介绍!


-测试环境
declare @inventory table (item varchar(20),color varchar(20),quantity money)
insert into @inventory select 'table','blue',124
insert into @inventory select 'table','red' ,223
insert into @inventory select 'chair','blue',101
insert into @inventory select 'chair','red' ,210

--查询
select case when (grouping(item) = 1) then '总计'
when (grouping(color) = 1) then item+'小计'
else isnull(item, 'unknown')
end as item,
case when (grouping(color) = 1) then ''
else isnull(color, 'unknown')
end as color,
sum(quantity) as qtysum
from @inventory
group by item, color with rollup

--结果
item color qtysum
------------------------ -------------------- ---------------------
chair blue 101.0000
chair red 210.0000
chair小计 311.0000
table blue 124.0000
table red 223.0000
table小计 347.0000
总计 658.0000

(所影响的行数为 7 行)



2005-10-10 9:44:39


修改笔记
发表评语»»» 2005-10-10 10:01:01 grouping

grouping
是一个聚合函数,它产生一个附加的列,当用 cube 或 rollup 运算符添加行时,附加的列输出值为1,当所添加的行不是由 cube 或 rollup 产生时,附加列值为0。

仅在与包含 cube 或 rollup 运算符的 group by 子句相联系的选择列表中才允许分组。

语法
grouping ( column_name )

参数
column_name

是 group by 子句中用于检查 cube 或 rollup 空值的列。

返回类型
int

注释
分组用于区分由 cube 和 rollup 返回的空值和标准的空值。作为cube 或 rollup 操作结果返回的 null 是 null 的特殊应用。它在结果集内作为列的占位符,意思是"全体"。


2005-10-10 10:02:45 用 rollup 汇总数据

用 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 rollup

item 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 生成的更为高效。


更多精彩文章浏览...
点击右上角图标分享到朋友圈
官方网站:http://www.clicksun.cn
咨询热线:400-186-1886
服务邮箱:service@clicksun.cn