最近由于要做报表,在一张表中有一个字段为date类型,现在想要在一段时间内(比如一年)能够按照时间段分组查询记录总和,比如我要能够查询2007年到2009年间按月份分组记录条数统计,网上提示用 group by to_char(date_column, 'yyy-mm'),构造如下sql语句:
select count(*), s.create_date as date_split
from sample s
where s.create_date between '2007-01' and '2009-12'
group by to_char(date_split, 'yyy-mm');
但是,我不只是要能按月份来分组,还要求能够按周和季度来分组,这种查询语句怎么构造呢,网上似乎没有这方面资料,望大虾教我,小弟不胜感激!
select count(*), s.create_date as date_split
from sample s
where s.create_date between '2007-01' and '2009-12'
group by to_char(date_split, 'yyy-mm');
但是,我不只是要能按月份来分组,还要求能够按周和季度来分组,这种查询语句怎么构造呢,网上似乎没有这方面资料,望大虾教我,小弟不胜感激!
解决方案 »
- OTL(Version 4.0.179) 出现段错误
- ORA-01033: ORACLE initialization or shutdown in progress
- 初学Oracle,请高手指教!
- 在oracle-SQL PLUS中,数据库、管理表空间和新用户的建立顺序
- 数据库启动不能,高手请进!
- 关于JOB的一个问题
- oracle连接问题!
- 关于Oracle中的时间计算问题!
- 一个很基础基础的问题:如何把数据库里的所有的表中的记录一次清空(不能删除表的)?
- 是读软件工程硕士还是去当程序员?
- OCCI setInt()不能绑定输入参数
- 请大家帮助分析一下是选Oracle还是选SQL Server
--------------
2009-09
2009-09
2009-09
2008-11
2008-11
2008-09
2009-05
2009-12
2009-12已选择9行。SQL> select to_char(b,'YYYY'),sum(decode(to_char(b,'MM'),'01',1,'02',1,'03',1,0)) as one,
2 sum(decode(to_char(b,'MM'),'04',1,'05',1,'06',1,0)) as two,
3 sum(decode(to_char(b,'MM'),'07',1,'08',1,'09',1,0)) as three,
4 sum(decode(to_char(b,'MM'),'10',1,'11',1,'12',1,0)) as four from table1
5 group by to_char(b,'YYYY');TO_CHAR( ONE TWO THREE FOUR
-------- ---------- ---------- ---------- ----------
2009 0 1 3 2
2008 0 0 1 2
from xxx
where date_column between '01-Jan-2007' and '31-Dec-2009'
group by to_char(date_column, 'yyyy-mm')
按季度来分组
select to_char(date_column, 'yyyy-Q'),count(*)
from xxx
where date_column between '01-Jan-2007' and '31-Dec-2009'
group by to_char(date_column, 'yyyy-Q')按周来分组
select to_char(date_column, 'yyyy-IW'),count(*)
from xxx
where date_column between '01-Jan-2007' and '31-Dec-2009'
group by to_char(date_column, 'yyyy-IW')
------------ ----------
2008-4 2
2009-3 3
2008-3 1
2009-2 1
2009-4 2