汇总数据操作符:
compute:使用聚合函数生成数据的汇总值.详细信息及所有行的总值.
compute by:后面加by不会生成一个总的汇总值.而是详细信息及每一类别的汇总值.
with rollup:可以创建 group by 子句元素内元素的汇总与分类汇总.
with cube:可以创建 group by子句中列表的所有可能的分组组合,生成超聚合行.
COPY了SQL 联机帮助里的一段:
-----------------------
ROLLUP 操作的结果集具有类似于 COMPUTE BY 所返回结果集的功能;然而,ROLLUP 具有下列优点:
ROLLUP 返回单个结果集;COMPUTE BY 返回多个结果集,而多个结果集会增加应用程序代码的复杂性。
ROLLUP 可以在服务器游标中使用;COMPUTE BY 不可以。
有时,查询优化器为 ROLLUP 生成的执行计划比为 COMPUTE BY 生成的更为高效
-------------------------
下面就是针对上面四个操作符的例子.首先把group by分组列出来对比:
use tsie
go
select * from syscolumns where id=object_id(N'OMaster')
--------------------------------
--group by:显示信息为每个销售单负责者每月每项物料的欠量量;
SELECT CONMer,datepart(MM,Curdate)as curMonth,PartNum,sum(DQty)
FROM SMaster s,OMaster o
where s.OrderNum=o.OrderNum
and Curdate<getdate()
and PartNum like '1-6-1%'
group by CONMer,datepart(MM,Curdate),PartNum
order by CONMer,datepart(MM,Curdate)
/*
CONMer curMonth PartNum
------------------------------ ----------- --------------- ---------------------
9 1-6-141 50000.0
FM 8 1-6-145 10000.0
FM 9 1-6-141 50000.0
FM 9 1-6-145 12850.0
JH 7 1-6-145 0.0
JH 8 1-6-141 8500.0
JH 8 1-6-145 21200.0
(所影响的行数为 7 行)
*/
--------------------------------
--compute by: 分类分组显示:具有详细信息,也会在每一组后给个汇总值.注意Compute by 后的列必须出现在语句的选择列中以及order by中,并且按顺序(order by中的)出现.否则将出现如下信息:
/*
服务器: 消息 163,级别 15,状态 1,行 7
计算依据列表与排序依据列表不匹配。
*/
SELECT CONMer,datepart(MM,Curdate)as curMonth,PartNum,DQty
FROM SMaster s,OMaster o
where s.OrderNum=o.OrderNum
and Curdate<getdate()
and PartNum like '1-6-1%'
order by CONMer,datepart(MM,Curdate),PartNum
compute sum(DQty) by CONMer,datepart(MM,Curdate),PartNum
--------------------------------
/*
CONMer curMonth PartNum DQty
------------------------------ ----------- --------------- -------------------------------
9 1-6-141 50000.0
sum
===================================
50000.0
CONMer curMonth PartNum DQty
------------------------------ ----------- --------------- -----------------------------------------------------
FM 8 1-6-145 10000.0
sum
==================================
10000.0
CONMer curMonth PartNum DQty
------------------------------ ----------- --------------- -----------------------------
FM 9 1-6-141 50000.0
sum
=================================
50000.0
CONMer curMonth PartNum DQty
------------------------------ ----------- --------------- ----------------------
FM 9 1-6-145 3250.0
FM 9 1-6-145 5400.0
FM 9 1-6-145 4200.0
sum
==============================
12850.0
CONMer curMonth PartNum DQty
------------------------------ ----------- --------------- ----------------------------
JH 7 1-6-145 0.0
JH 7 1-6-145 0.0
sum
==============================
0.0
CONMer curMonth PartNum DQty
------------------------------ ----------- --------------- -------------------------
JH 8 1-6-141 8500.0
sum
==============================
8500.0
CONMer curMonth PartNum DQty
------------------------------ ----------- --------------- -------------------------------
JH 8 1-6-145 3200.0
JH 8 1-6-145 18000.0
JH 8 1-6-145 0.0
sum
================================
21200.0
(所影响的行数为 19 行)
*/
--------------------------------
--compute:
SELECT CONMer,datepart(MM,Curdate)as curMonth,PartNum,DQty
FROM SMaster s,OMaster o
where s.OrderNum=o.OrderNum
and Curdate<getdate()
and PartNum like '1-6-1%'
order by CONMer,datepart(MM,Curdate),PartNum
compute sum(DQty)
--------------------------------
/*
CONMer curMonth PartNum DQty
------------------------------ ----------- --------------- -----------------------
9 1-6-141 50000.0
FM 8 1-6-145 10000.0
FM 9 1-6-141 50000.0
FM 9 1-6-145 3250.0
FM 9 1-6-145 5400.0
FM 9 1-6-145 4200.0
JH 7 1-6-145 0.0
JH 7 1-6-145 0.0
JH 8 1-6-141 8500.0
JH 8 1-6-145 3200.0
JH 8 1-6-145 18000.0
JH 8 1-6-145 0.0
sum
===============================
152550.0
(所影响的行数为 13 行)
*/
--------------------------------
---rollup:给每一个Group by分组进行汇总.
use tsie
go
select CONMer,datepart(MM,Curdate)as curMonth,PartNum,sum(DQty)
FROM SMaster s,OMaster o
where s.OrderNum=o.OrderNum
and Curdate <getdate()
and PartNum like'1-6-1%'
group by CONMer,datepart(MM,Curdate),PartNum with rollup
--------------------------------
/*
CONMer curMonth PartNum DQty
------------------------------ ----------- --------------- --------------------------
9 1-6-141 50000.0
9 NULL 50000.0
NULL NULL 50000.0
FM 8 1-6-145 10000.0
FM 8 NULL 10000.0
FM 9 1-6-141 50000.0
FM 9 1-6-145 12850.0
FM 9 NULL 62850.0
FM NULL NULL 72850.0
JH 7 1-6-145 0.0
JH 7 NULL 0.0
JH 8 1-6-141 8500.0
JH 8 1-6-145 21200.0
JH 8 NULL 29700.0
JH NULL NULL 29700.0
NULL NULL NULL 152550.0 --所有CONMer的欠量汇总
(所影响的行数为 16 行)
*/
--------------------------------
--cube:所有group by中可能的组合汇总:
select CONMer,datepart(MM,Curdate)as curMonth,PartNum,sum(DQty)
FROM SMaster s,OMaster o
where s.OrderNum=o.OrderNum
and Curdate <getdate()
and PartNum like '1-6-1%'
group by CONMer,datepart(MM,Curdate),PartNum with cube
--------------------------------
/*
CONMer curMonth PartNum
------------------------------ ----------- --------------- ---------------------------------
9 1-6-141 50000.0
9 NULL 50000.0
NULL NULL 50000.0
FM 8 1-6-145 10000.0
FM 8 NULL 10000.0
FM 9 1-6-141 50000.0
FM 9 1-6-145 12850.0
FM 9 NULL 62850.0
FM NULL NULL 72850.0
JH 7 1-6-145 0.0
JH 7 NULL 0.0
JH 8 1-6-141 8500.0
JH 8 1-6-145 21200.0
JH 8 NULL 29700.0
JH NULL NULL 29700.0
NULL NULL NULL 152550.0
NULL 7 1-6-145 0.0
NULL 7 NULL 0.0
NULL 8 1-6-141 8500.0
NULL 8 1-6-145 31200.0
NULL 8 NULL 39700.0
NULL 9 1-6-141 100000.0
NULL 9 1-6-145 12850.0
NULL 9 NULL 112850.0
NULL 1-6-141 50000.0
FM NULL 1-6-141 50000.0
JH NULL 1-6-141 8500.0
NULL NULL 1-6-141 108500.0
FM NULL 1-6-145 22850.0
JH NULL 1-6-145 21200.0
NULL NULL 1-6-145 44050.0
(所影响的行数为 31 行)
*/
--------------------------------