日期:2014-05-17  浏览次数:20745 次

按季度或周或旬汇总的问题
在oracle中如何实现以上的问题?

例如有表:

    ADate                   AValue
    2006-12-5           8
    2007-1-1             10
    2007-3-8             110
    2007-5-10           18
    .....

    如何得到:
    2006四季度         8
    2007一季度         120
    2007二季度         18
    ....

------解决方案--------------------
select trunc(t.adate, 'Q '),sum(t.avalue) from t
group by trunc(t.adate, 'Q ')
------解决方案--------------------
select to_char(t.adate, 'yyyy ')|| '年 '||to_char(sysdate, 'Q ')|| '季度 ',sum(t.avalue) from t
group by to_char(t.adate, 'Q ')

------解决方案--------------------
SELECT to_char(ADate, 'yyyy ')|| '年 '||to_char(ADate, 'q ')|| '季度 ' as 季度,sum(AValue) as total from tablename
group by to_char(ADate, 'yyyy ')|| '年 '||to_char(ADate, 'q ')|| '季度 '

SELECT to_char(ADate, 'yyyy ')|| '年 '||to_char(ADate, 'iw ')|| '周 ' as 周,sum(AValue) as total from tablename
group by to_char(ADate, 'yyyy ')|| '年 '||to_char(ADate, 'iw ')|| '周 '

SELECT to_char(ADate, 'yyyy ')|| '年 '||to_char(ADate, 'mm ')|| '月 '
|| case when to_char(ADate, 'dd ') <=10 then '上旬 ' when to_char(ADate, 'dd ')> 20 then '下旬 ' else '中旬 ' end
as 旬,sum(AValue) as total from tablename
group by to_char(ADate, 'yyyy ')|| '年 '||to_char(ADate, 'mm ')|| '月 '
|| case when to_char(ADate, 'dd ') <=10 then '上旬 ' when to_char(ADate, 'dd ')> 20 then '下旬 ' else '中旬 ' end