日期:2014-05-17 浏览次数:21004 次
select to_char(c1,'q') 季度,nvl(sum(price),0) 消费金额 from
(select add_months(date'2011-12-01',level) c1
from dual
connect by level <= 12) a left join sell b on to_char(c1,'yyyy-mm')=to_char(dt,'yyyy-mm')
group by to_char(c1,'q')
order by to_char(c1,'q')
季度 消费金额
----------------------------
1 1 262
2 2 1200
3 3 5
4 4 0
------解决方案--------------------
select jd, sum(price)
from (select id,
name,
dt,
price,
case
when dt between to_date('2012-01-01', 'yyyy-mm-dd') and
to_date('2012-04-01', 'yyyy-mm-dd') then
'1季度'
when dt between to_date('2012-04-01', 'yyyy-mm-dd') and
to_date('2012-07-01', 'yyyy-mm-dd') then
'2季度'
when dt between to_date('2012-07-01', 'yyyy-mm-dd') and
to_date('2012-10-01', 'yyyy-mm-dd') then
'3季度'
when dt between to_date('2012-10-01', 'yyyy-mm-dd') and
to_date('2013-01-01', 'yyyy-mm-dd') then
'4季度'
end jd
from sell)
group by jd
JD SUM(PRICE)
----- ----------
1季度 262
2季度 1200
3季度 5
select sum(case
when dt between to_date('2012-01-01', 'yyyy-mm-dd') and
to_date('2012-04-01', 'yyyy-mm-dd') then
price
else
0
end) "1季度",
sum(case
when dt between to_date('2012-04-01', 'yyyy-mm-dd') and
to_date('2012-07-01', 'yyyy-mm-dd') then
price
else
0
end) "2季度",
sum(case
when dt between to_date('2012-07-01', 'yyyy-mm-dd') and
to_date('2012-10-01', 'yyyy-mm-dd') then
price
else
0
end) "3季度",
sum(case
when dt between to_date('2012-10-01', 'yyyy-mm-dd') and
to_date('2013-01-01', 'yyyy-mm-dd') then
price
else
0
end) "4季度"
from sell t
1季度 2季度 3季度 4季度
---------- ---------- ---------- ----------
262 1200 5 0
------解决方案--------------------
/*表一
季度 消费金额
*/
with quarter as --4个季度
(select level q1 from dual connect by level <= 4),
sell_q as --某年度下,各季度汇总
(select to_char(dt, 'Q') q2, sum(price) sum_p
from sell
where dt between date '2012-01-01' and dat