博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Oracle Group By ROLLUP-SubTotal
阅读量:7059 次
发布时间:2019-06-28

本文共 2306 字,大约阅读时间需要 7 分钟。

At normal time, you may want to have the subtotal for each region over all months, along with the total for all regions, or you may want the subtotal for each month over all regions, along with the total for all months. In short, you may need to generate subtotals and totals at more than one level.

 

Lets see how you go about doing this without using ROLLUP.

SELECT reg.region,
   TO_CHAR
(TO_DATE
(ord.month,
'MM'
),
'Month'
) month, SUM
(ord.total_sales
)
FROM orders ord, region reg
WHERE ord.region_id = reg.region_id
GROUP
BY reg.region, ord.month;
UNION
ALL
SELECT reg.region,  
NULL, SUM
(ord.total_sales
)
FROM orders ord, region reg
WHERE ord.region_id = reg.region_id
GROUP
BY reg.region
UNION
ALL
SELECT
NULL,
NULL, SUM
(ord.total_sales
)
FROM orders ord, region reg
WHERE ord.region_id = reg.region_id;

 

Lets see how we can accomplish the same results using Group By ROLLUP:

 

SELECT reg.region,
   TO_CHAR
(TO_DATE
(ord.month,
'MM'
),
'Month'
) month, SUM
(ord.total_sales
)
FROM orders ord, region reg
WHERE ord.region_id = reg.region_id
GROUP
BY ROLLUP
(reg.region, ord.month
);
 

REGION               MONTH     SUM(ORD.TOT_SALES)

-------------------- --------- ----------------
New England          January            1527645
New England          February           1847238
New England          March              1699449
New England          April              1792866
New England          May                1698855
New England          June               1510062
New England          July               1678002
New England          August             1642968
New England          September          1726767
New England          October            1648944
New England          November           1384185
New England          December           1599942
New England                            19756923
Mid-Atlantic         January            1832091
Mid-Atlantic         February           1286028
Mid-Atlantic         March              1911093
Mid-Atlantic         April              1623438
Mid-Atlantic         May                1778805
Mid-Atlantic         June               1504455
Mid-Atlantic         July               1820742
Mid-Atlantic         August             1381560
Mid-Atlantic         September          1178694
Mid-Atlantic         October            1530351
Mid-Atlantic         November           1598667
Mid-Atlantic         December           1477374
Mid-Atlantic                           18923298
Southeast US         January            1137063
Southeast US         February           1855269
Southeast US         March              1967979
Southeast US         April              1830051
Southeast US         May                1983282
Southeast US         June               1705716
Southeast US         July               1670976
Southeast US         August             1436295
Southeast US         September          1905633
Southeast US         October            1610523
Southeast US         November           1661598
Southeast US         December           1841100
Southeast US                           20605485
                                       59285706

转载于:https://www.cnblogs.com/GoDevil/archive/2008/08/11/1265328.html

你可能感兴趣的文章
动态编译
查看>>
分享:一个基于NPOI的excel导入导出组件(强类型)
查看>>
数据结构实验之二叉树的建立与遍历
查看>>
C++基础之迭代器
查看>>
杂题 洛谷P2018 消息传递
查看>>
算法笔记 --- Radix Sort
查看>>
Jetty的配置
查看>>
scala函数等号省略
查看>>
通过AutoConfig实现Form Server配置文件的修改 【转载】
查看>>
20165324 2017-2018-2 《Java程序设计》课程总结
查看>>
8-unittest中case管理
查看>>
ExtJs XTemplate
查看>>
[转载[工具]]PLSQL使用技巧
查看>>
用户管理的设计--1.首页查询功能实现
查看>>
PHP保留两位小数
查看>>
numpy 数组相减
查看>>
getRequestDispatcher(path).forward(),,执行完,后面的代码居然还会执行!!!记得加return 啊亲...
查看>>
如何安装和配置RabbitMQ
查看>>
08-jQuery的位置信息
查看>>
MFC树控件CTreeCtrl
查看>>