Oracle按不同时间分组统计

Oracle按不同时间分组统计的sql

复制代码
如下表table1: 
日期(exportDate)               数量(amount) 
--------------                    ----------- 
14-2月 -08                       20 
10-3月 -08                       2 
14-4月 -08                       6 
14-6月 -08                       75 
24-10月-09                      23 
14-11月-09                      45 
04-8月 -10                       5 
04-9月 -10                       44 
04-10月-10                      88 
注意:为了显示更直观,如下查询已皆按相应分组排序 
1.按年份分组 
select to_char(exportDate,'yyyy'),sum(amount) from table1 group by to_char(exportDate,'yyyy'); 
年份      数量 
----------------------------- 
2009    68 
2010    137 
2008    103 
2.按月份分组 
select to_char(exportDate,'yyyy-mm'),sum(amount) from table1 group by to_char(exportDate,'yyyy-mm')
order by to_char(exportDate,'yyyy-mm'); 
月份           数量 
----------------------------- 
2008-02    20 
2008-03    2 
2008-04    6 
2008-06    75 
2009-10    23 
2009-11    45 
2010-08    5 
2010-09    44 
2010-10    88 
3.按季度分组 
select to_char(exportDate,'yyyy-Q'),sum(amount) from table1 group by to_char(exportDate,'yyyy-Q') 
order by to_char(exportDate,'yyyy-Q'); 
季度          数量 
------------------------------ 
2008-1    22 
2008-2    81 
2009-4    68 
2010-3    49 
2010-4    88 
4.按周分组 
select to_char(exportDate,'yyyy-IW'),sum(amount) from table1 group by to_char(exportDate,'yyyy-IW')
order by to_char(exportDate,'yyyy-IW'); 
周             数量 
------------------------------ 
2008-07    20 
2008-11    2 
2008-16    6 
2008-24    75 
2009-43    23 
2009-46    45 
2010-31    5 
2010-35    44 
2010-40    88
复制代码

出处:http://www.2cto.com/database/201309/243555.html

posted on   jack_Meng  阅读(3598)  评论(1编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

支付宝打赏

主题色彩