LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

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

admin
2010年6月27日 17:33 本文热度 6608


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

统计报表(用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 行)


[align=right]2005-10-10 9:44:39 [/align]


修改笔记
发表评语»»» 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 的特殊应用。它在结果集内作为列的占位符,意思是"全体"。

[align=right] [/align] 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 生成的更为高效。


该文章在 2010/6/27 17:33:22 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved