Sql 查询中的周/月/季度分组

Sql 查询中的周/月/季度分组,sql,oracle,time,grouping,Sql,Oracle,Time,Grouping,假设我有一个包含以下列的表 1. Client - string. 2. Profit - integer. 3. Deal_Date - date. 我需要查询,将检索按周/月/季度等利润细分的总和 数周的预期产量 1 row, sum (profit) of all deals that registered from (03.19.2012 - 03.12.2012). 2 row, sum (profit) of all deals that registered from (03.1

假设我有一个包含以下列的表

1. Client - string.
2. Profit - integer.
3. Deal_Date - date.
我需要查询,将检索按周/月/季度等利润细分的总和

数周的预期产量

1 row, sum (profit) of all deals that registered from (03.19.2012 - 03.12.2012).
2 row, sum (profit) of all deals that registered from (03.12.2012 - 03.05.2012).
...
n row, sum (profit) of all deals that registered from (05.17.2011 - 05.10.2011).
注(日期设置仅为示例)

月份、年份等都一样

有人能帮我解答这个问题吗


顺便说一句,性能非常重要

此查询使用简单的日期格式提取要跟踪的各种元素,并进行分析以获得总和

select client
       , yr
       , qtr
       , wk
       , sum ( profit ) over ( partition by client, yr) as yr_profit
       , sum ( profit ) over ( partition by client, yr, qtr) as qtr_profit
       , sum ( profit ) over ( partition by client, yr, wk) as wk_profit
from ( 
         select client
                , profit
                , to_char(deal_date, 'yyyy') as yr
                , to_char(deal_date, 'q') as qt
                , to_char(deal_date, 'ww') as wk
          from your_table )
/
这将为当前表中的每一行生成一行。因此,您可能希望将其包装在一个只返回不同行的进一步外部查询中

一种变体是使用汇总。我不确定在分组标准没有完全分层的情况下(周不能很好地划分为季度)这种方法的效果如何


只需创建一个SP,并根据需要为每周、每月或每年循环代码

你能展示一下你想要的输出吗?您需要表示什么日期范围?当你说“绩效非常重要”时,你的标准是什么?你说的是多少数据?1997年可能重复这一点是一个糟糕的答案,但现在一排又一排令人痛苦的数据是不可接受的。甲骨文在过去的15年里确实取得了进步,有很多不同的、性能更好的选择。
select client
       , yr
       , qtr
       , wk
       , sum ( profit ) as profit
from ( 
         select client
                , profit
                , to_char(deal_date, 'yyyy') as yr
                , to_char(deal_date, 'q') as qt
                , to_char(deal_date, 'ww') as wk
          from your_table )
group by rollup ( client, yr, qtr, wk )
/