如何在一个季度内对月份和组月进行分区

时间:2018-03-03 14:45:10

标签: sql oracle

我想在几个月的列上应用分区,并在四分之一的时间内对它们进行分组。

Months

Jan-18
Feb-18
Mar-18
Apr-18
May-18
Jun-18
Jul-18
Aug-18
Sep-18
Oct-18
Nov-18
Dec-18

我的日历从Jun开始

所以

Jun,Jul,Aug is considered as 1st quarter .
Sep,Oct,Nov is considered as 2nd quarter .
Dec,Jan,Feb is considered as 3rd quarter .
Mar,Apr,May is considered as 4th quarter .

trunc(Jan-18,'q')returns 01-Jan-18

whereas expected result is '01-Nov-18' .

我试过

select ADD_MONTHS(trunc(to_date('01-APR-18'), 'Q'),2) from dual;

也尝试了

select ADD_MONTHS(trunc(to_date('01-JUN-18'), 'Q'),-1) from dual;
output is '01-Mar-18',whereas expected reuslt is '01-JUN-18' .

预期结果集:

 Months     Expected result 

02-Jan-18      01-Dec-17
02-Feb-18      01-Dec-17
05-Mar-18      01-Mar-18
08-Apr-18      01-Mar-18
05-May-18      01-Mar-18
05-Jun-18      01-Jun-18
05-Jul-18      01-Jun-18
09-Aug-18      01-Jun-18
10-Sep-18      01-Sep-18
11-Oct-18      01-Sep-18
11-Nov-18      01-Sep-18
11-Dec-18      01-Dec-18

有人可以帮忙吗

2 个答案:

答案 0 :(得分:1)

您尝试的解决方案的问题在于您错过了一个步骤。

如果您的宿舍离开“标准”宿舍一个月,那么 - 给定日期,任何日期 - 您必须将日期提前一个月,然后截断到该季度然后拉回一个月。您执行了第2步和第3步,但是您错过了第1步。

with
  inputs ( dt ) as (
    select to_date('02-Jan-18', 'dd-Mon-yy') from dual union all
    select to_date('02-Feb-18', 'dd-Mon-yy') from dual union all
    select to_date('05-Mar-18', 'dd-Mon-yy') from dual union all
    select to_date('08-Apr-18', 'dd-Mon-yy') from dual union all
    select to_date('05-May-18', 'dd-Mon-yy') from dual union all
    select to_date('05-Jun-18', 'dd-Mon-yy') from dual union all
    select to_date('05-Jul-18', 'dd-Mon-yy') from dual union all
    select to_date('09-Aug-18', 'dd-Mon-yy') from dual union all
    select to_date('10-Sep-18', 'dd-Mon-yy') from dual union all
    select to_date('11-Oct-18', 'dd-Mon-yy') from dual union all
    select to_date('11-Nov-18', 'dd-Mon-yy') from dual union all
    select to_date('11-Dec-18', 'dd-Mon-yy') from dual
  )
select dt,
       add_months(trunc(add_months(dt, 1), 'q'), -1) as qtr
from   inputs;

DT          QTR     
---------   ---------
02-Jan-18   01-Dec-17
02-Feb-18   01-Dec-17
05-Mar-18   01-Mar-18
08-Apr-18   01-Mar-18
05-May-18   01-Mar-18
05-Jun-18   01-Jun-18
05-Jul-18   01-Jun-18
09-Aug-18   01-Jun-18
10-Sep-18   01-Sep-18
11-Oct-18   01-Sep-18
11-Nov-18   01-Sep-18
11-Dec-18   01-Dec-18

答案 1 :(得分:0)

你能使用case吗?

select (case when extract(month from <date>) in (6, 7, 8) then 1
             when extract(month from <date>) in (9, 10, 11) then 2
             when extract(month from <date>) in (12 1, 2) then 3
             when extract(month from <date>) in (3, 4, 5) then 4
        end) as quarter

你可以使用这样的内置函数来获得季度:

select to_char(add_months(<date>, -5), 'Q')

这也适用于trunc()

select trunc(add_months(<date>, -5), 'Q')

如果你想要的是开始日期,你可以使用case(这是最明确和最容易理解的)或做花式算术:

select add_months(add_months(trunc(add_months(<date>, -5), 'YYYY'), 6),
                  to_number(to_char(add_months(<date>, -5), 'Q')) * 3 - 4
                 )

这增加了自6月1日以来应该是您所在会计年度的季度数。

这里有一点rextester