select count(id),time From table where time>yy-01-01 and time <yy-07-01 group by time
with t as (select 1 as id,'2012-01-01' as times from dual union all select 2 as id,'2012-02-01' as times from dual union all select 3 as id,'2012-03-01' as times from dual union all select 4 as id,'2012-04-01' as times from dual union all select 5 as id,'2012-06-01' as times from dual union all select 6 as id,'2012-07-01' as times from dual union all select 7 as id,'2012-08-01' as times from dual union all select 8 as id,'2012-09-01' as times from dual union all select 9 as id,'2012-10-01' as times from dual union all select 11 as id,'2011-01-01' as times from dual union all select 12 as id,'2011-02-01' as times from dual union all select 13 as id,'2011-03-01' as times from dual union all select 14 as id,'2011-04-01' as times from dual union all select 15 as id,'2011-06-01' as times from dual union all select 16 as id,'2011-07-01' as times from dual union all select 17 as id,'2011-08-01' as times from dual union all select 18 as id,'2011-09-01' as times from dual union all select 19 as id,'2011-10-01' as times from dual)Select Count(1),dates From ( select id,case when to_char(to_date(times,'yyyy-mm-dd'),'mm')>='06' then to_char(to_date(times,'yyyy-mm-dd'),'yyyy')||'上半年' else to_char(to_date(times,'yyyy-mm-dd'),'yyyy')||'下半年' end as dates from t) group by dates order by 2;
with t as (select 1 as id,'2012-01-01' as times from dual union all select 2 as id,'2012-02-01' as times from dual union all select 3 as id,'2012-03-01' as times from dual union all select 4 as id,'2012-04-01' as times from dual union all select 5 as id,'2012-06-01' as times from dual union all select 6 as id,'2012-07-01' as times from dual union all select 7 as id,'2012-08-01' as times from dual union all select 8 as id,'2012-09-01' as times from dual union all select 9 as id,'2012-10-01' as times from dual union all select 11 as id,'2011-01-01' as times from dual union all select 12 as id,'2011-02-01' as times from dual union all select 13 as id,'2011-03-01' as times from dual union all select 14 as id,'2011-04-01' as times from dual union all select 15 as id,'2011-06-01' as times from dual union all select 16 as id,'2011-07-01' as times from dual union all select 17 as id,'2011-08-01' as times from dual union all select 18 as id,'2011-09-01' as times from dual union all select 19 as id,'2011-10-01' as times from dual)Select Count(1)as cnt, dates From (select id, case when to_char(to_date(times, 'yyyy-mm-dd'), 'mm') <='03' then to_char(to_date(times, 'yyyy-mm-dd'), 'yyyy') || '年 第1季度' when to_char(to_date(times, 'yyyy-mm-dd'), 'mm') <='06' then to_char(to_date(times, 'yyyy-mm-dd'), 'yyyy') || '年 第2季度' when to_char(to_date(times, 'yyyy-mm-dd'), 'mm') <='09' then to_char(to_date(times, 'yyyy-mm-dd'), 'yyyy') || '年 第3季度' when to_char(to_date(times, 'yyyy-mm-dd'), 'mm') <='12' then to_char(to_date(times, 'yyyy-mm-dd'), 'yyyy') || '年 第4季度' end as dates from t) group by dates order by 2这个是按季度的,都测试过了
数据参考ls select substr(times,1,4) 年份,to_char(to_date(times,'yyyy-mm-dd'),'Q') 季度,count(*) 数量 from t group by substr(times,1,4),to_char(to_date(times,'yyyy-mm-dd'),'Q') order by substr(times,1,4),to_char(to_date(times,'yyyy-mm-dd'),'Q') 年份 季度 数量 ------------------------------- 1 2011 1 3 2 2011 2 2 3 2011 3 3 4 2011 4 1 5 2012 1 3 6 2012 2 2 7 2012 3 3 8 2012 4 1
玩玩分析函数with t as (select 1 as id, '2012-01-01' as times from dual union all select 2 as id, '2012-02-01' as times from dual union all select 3 as id, '2012-03-01' as times from dual union all select 4 as id, '2012-04-01' as times from dual union all select 5 as id, '2012-06-01' as times from dual union all select 6 as id, '2012-07-01' as times from dual union all select 7 as id, '2012-08-01' as times from dual union all select 8 as id, '2012-09-01' as times from dual union all select 9 as id, '2012-10-01' as times from dual union all select 11 as id, '2011-01-01' as times from dual union all select 12 as id, '2011-02-01' as times from dual union all select 13 as id, '2011-03-01' as times from dual union all select 14 as id, '2011-04-01' as times from dual union all select 15 as id, '2011-06-01' as times from dual union all select 16 as id, '2011-07-01' as times from dual union all select 17 as id, '2011-08-01' as times from dual union all select 18 as id, '2011-09-01' as times from dual union all select 19 as id, '2011-10-01' as times from dual) select a as "年份", b as "季度", MAX(c) as "数量" from (Select first_value(substr(times, 1, 4)) over(partition by substr(times, 1, 4) order by id) as a, first_value(to_char(to_date(times, 'yyyy-mm-dd'), 'Q')) over(partition by to_char(to_date(times, 'yyyy-mm-dd'), 'Q') order by times) as b, count(*) over(partition by substr(times, 1, 4), to_char(to_date(times, 'yyyy-mm-dd'), 'Q') order by times) as c from t) tt group by a,b order by 1, 2
(select 1 as id,'2012-01-01' as times from dual
union all
select 2 as id,'2012-02-01' as times from dual
union all
select 3 as id,'2012-03-01' as times from dual
union all
select 4 as id,'2012-04-01' as times from dual
union all
select 5 as id,'2012-06-01' as times from dual
union all
select 6 as id,'2012-07-01' as times from dual
union all
select 7 as id,'2012-08-01' as times from dual
union all
select 8 as id,'2012-09-01' as times from dual
union all
select 9 as id,'2012-10-01' as times from dual
union all
select 11 as id,'2011-01-01' as times from dual
union all
select 12 as id,'2011-02-01' as times from dual
union all
select 13 as id,'2011-03-01' as times from dual
union all
select 14 as id,'2011-04-01' as times from dual
union all
select 15 as id,'2011-06-01' as times from dual
union all
select 16 as id,'2011-07-01' as times from dual
union all
select 17 as id,'2011-08-01' as times from dual
union all
select 18 as id,'2011-09-01' as times from dual
union all
select 19 as id,'2011-10-01' as times from dual)Select Count(1),dates From (
select id,case when to_char(to_date(times,'yyyy-mm-dd'),'mm')>='06' then to_char(to_date(times,'yyyy-mm-dd'),'yyyy')||'上半年' else to_char(to_date(times,'yyyy-mm-dd'),'yyyy')||'下半年' end as dates from t)
group by dates
order by 2;
(select 1 as id,'2012-01-01' as times from dual
union all
select 2 as id,'2012-02-01' as times from dual
union all
select 3 as id,'2012-03-01' as times from dual
union all
select 4 as id,'2012-04-01' as times from dual
union all
select 5 as id,'2012-06-01' as times from dual
union all
select 6 as id,'2012-07-01' as times from dual
union all
select 7 as id,'2012-08-01' as times from dual
union all
select 8 as id,'2012-09-01' as times from dual
union all
select 9 as id,'2012-10-01' as times from dual
union all
select 11 as id,'2011-01-01' as times from dual
union all
select 12 as id,'2011-02-01' as times from dual
union all
select 13 as id,'2011-03-01' as times from dual
union all
select 14 as id,'2011-04-01' as times from dual
union all
select 15 as id,'2011-06-01' as times from dual
union all
select 16 as id,'2011-07-01' as times from dual
union all
select 17 as id,'2011-08-01' as times from dual
union all
select 18 as id,'2011-09-01' as times from dual
union all
select 19 as id,'2011-10-01' as times from dual)Select Count(1)as cnt, dates
From (select id,
case
when to_char(to_date(times, 'yyyy-mm-dd'), 'mm') <='03' then
to_char(to_date(times, 'yyyy-mm-dd'), 'yyyy') || '年 第1季度'
when to_char(to_date(times, 'yyyy-mm-dd'), 'mm') <='06' then
to_char(to_date(times, 'yyyy-mm-dd'), 'yyyy') || '年 第2季度'
when to_char(to_date(times, 'yyyy-mm-dd'), 'mm') <='09' then
to_char(to_date(times, 'yyyy-mm-dd'), 'yyyy') || '年 第3季度'
when to_char(to_date(times, 'yyyy-mm-dd'), 'mm') <='12' then
to_char(to_date(times, 'yyyy-mm-dd'), 'yyyy') || '年 第4季度'
end as dates
from t)
group by dates
order by 2这个是按季度的,都测试过了
select substr(times,1,4) 年份,to_char(to_date(times,'yyyy-mm-dd'),'Q') 季度,count(*) 数量
from t
group by substr(times,1,4),to_char(to_date(times,'yyyy-mm-dd'),'Q')
order by substr(times,1,4),to_char(to_date(times,'yyyy-mm-dd'),'Q') 年份 季度 数量
-------------------------------
1 2011 1 3
2 2011 2 2
3 2011 3 3
4 2011 4 1
5 2012 1 3
6 2012 2 2
7 2012 3 3
8 2012 4 1
(select 1 as id, '2012-01-01' as times
from dual
union all
select 2 as id, '2012-02-01' as times
from dual
union all
select 3 as id, '2012-03-01' as times
from dual
union all
select 4 as id, '2012-04-01' as times
from dual
union all
select 5 as id, '2012-06-01' as times
from dual
union all
select 6 as id, '2012-07-01' as times
from dual
union all
select 7 as id, '2012-08-01' as times
from dual
union all
select 8 as id, '2012-09-01' as times
from dual
union all
select 9 as id, '2012-10-01' as times
from dual
union all
select 11 as id, '2011-01-01' as times
from dual
union all
select 12 as id, '2011-02-01' as times
from dual
union all
select 13 as id, '2011-03-01' as times
from dual
union all
select 14 as id, '2011-04-01' as times
from dual
union all
select 15 as id, '2011-06-01' as times
from dual
union all
select 16 as id, '2011-07-01' as times
from dual
union all
select 17 as id, '2011-08-01' as times
from dual
union all
select 18 as id, '2011-09-01' as times
from dual
union all
select 19 as id, '2011-10-01' as times from dual)
select a as "年份", b as "季度", MAX(c) as "数量"
from (Select first_value(substr(times, 1, 4)) over(partition by substr(times, 1, 4) order by id) as a,
first_value(to_char(to_date(times, 'yyyy-mm-dd'), 'Q')) over(partition by to_char(to_date(times, 'yyyy-mm-dd'), 'Q') order by times) as b,
count(*) over(partition by substr(times, 1, 4), to_char(to_date(times, 'yyyy-mm-dd'), 'Q') order by times) as c
from t) tt
group by a,b
order by 1, 2