查找ORACLE中最后一个季度的第一天和最后一天

时间:2011-09-08 12:45:06

标签: sql date

我对表格有疑问:

select *
from X
where <some_date is in the last quarter>

我在收到上一季度的正确日期时遇到了麻烦。所以,说当前日期是 7月1日,即在第三季度,我希望将 4月1日作为FIRST和 6月30日上个季度的最后一天(即第二季度)。

谷歌搜索了一下,发现了大量的解决方案,但每一个都涵盖了SQL Server,我们的ORACLE数据库(Oracle 10g和11g)上没有可用的时髦方法。

哦,是的,我还需要能够将整个事情放在一个查询中,因为这是一个限制,通过一些工具可以进一步使用此查询...:/

8 个答案:

答案 0 :(得分:12)

这个更简单,但可能仍然不是最简单的方法:

SELECT
  ADD_MONTHS(TRUNC(SYSDATE, 'Q'), -3) AS First,
  TRUNC(SYSDATE, 'Q') - 1 AS Last
FROM DUAL

也许你也可以使用像这样的子选择来排除一些重复的代码:

SELECT
  ADD_MONTHS(D, -3) AS First,
  D - 1 AS Last
FROM (SELECT TRUNC(SYSDATE, 'Q') AS D FROM DUAL)

答案 1 :(得分:3)

SELECT
  TRUNC(SYSDATE, 'Q')AS FIRST_DAY,
  last_day(add_months(TRUNC(SYSDATE, 'Q'),2)) as LAST_DAY                
FROM DUAL;

答案 2 :(得分:1)

SELECT MIN (t), MAX (LAST_DAY (t))
  FROM (    SELECT ADD_MONTHS (TRUNC (SYSDATE, 'yyyy'), LEVEL - 1) t,
                   TO_CHAR (ADD_MONTHS (TRUNC (SYSDATE, 'yyyy'), LEVEL - 1), 'Q')
                      r
              FROM DUAL
        CONNECT BY LEVEL <= 12) a
 WHERE a.r = 4;

答案 3 :(得分:0)

典型的,只要我寻求帮助,我就会找到一些博客,让我知道如何继续下去。

管理一起敲响一些声明,但这绝对是丑陋的。 :)

select
    TRUNC(ADD_MONTHS(sysdate, -3),'Q') as first,
    LAST_DAY(TRUNC(ADD_MONTHS(sysdate, -3),'Q')+ 85) as last
from dual;

这看起来很有效,但如果有人知道更好的解决方案,请告诉我! (to_date('27-JAN-11')作为示例日期......)

编辑:修正了一个错误 - 在一个季度的第一天增加3个月并不总是在同一季度结束。现在它更加丑陋 - 该死的格里高利历!

答案 4 :(得分:0)

这是一种方法,它省去了必须计算出第一个和最后一个日期并将结果放在主查询的where子句中的麻烦:

select
    *,
    round(to_number(to_char(some_date, 'mm')) / 4) as quarter
from x
where round(to_number(to_char(some_date, 'mm')) / 4) = round(to_number(to_char(sysdate, 'mm')) / 4)

答案 5 :(得分:0)

SELECT DATE_CURRENT
     , TRUNC (ADD_MONTHS (DATE_CURRENT, -6), 'Q')                  AS FIRST
     , LAST_DAY (TRUNC (ADD_MONTHS (DATE_CURRENT, -6), 'Q') + 85)  AS LAST
     , LAST_DAY (ADD_MONTHS(trunc(DATE_CURRENT,'Q'),-1))                  AS PREVIOUS_QUARTER_END
     , ADD_MONTHS(LAST_DAY (ADD_MONTHS(trunc(DATE_CURRENT,'Q'),-1)),-3)+1 AS PREVIOUS_QUARTER_START
FROM
       (
             SELECT TO_DATE ('31.07.2014', 'DD.MM.YYYY') AS DATE_CURRENT FROM DUAL
       UNION SELECT TO_DATE ('30.06.2014', 'DD.MM.YYYY') AS DATE_CURRENT FROM DUAL
       UNION SELECT TO_DATE ('30.04.2014', 'DD.MM.YYYY') AS DATE_CURRENT FROM DUAL
       UNION SELECT TO_DATE ('31.03.2014', 'DD.MM.YYYY') AS DATE_CURRENT FROM DUAL
       )
order by DATUM;

答案 6 :(得分:0)

SELECT DATE_CURRENT      ,TRUNC(DATE_CURRENT,'Q')AS Q1F      ,LAST_DAY(ADD_MONTHS(TRUNC(DATE_CURRENT,'Q'),2))AS Q1L      ,LAST_DAY(ADD_MONTHS(trunc(DATE_CURRENT,'Q'), - 1))AS Q2F      ,ADD_MONTHS(LAST_DAY(ADD_MONTHS(trunc(DATE_CURRENT,'Q'), - 1)), - 3)+1 AS Q2L           ,LAST_DAY(ADD_MONTHS(trunc(DATE_CURRENT,'Q'), - 4))AS Q3F      ,ADD_MONTHS(LAST_DAY(ADD_MONTHS(trunc(DATE_CURRENT,'Q'), - 4)), - 3)+1 AS Q3L                ,LAST_DAY(ADD_MONTHS(trunc(DATE_CURRENT,'Q'), - 7))AS Q4F      ,ADD_MONTHS(LAST_DAY(ADD_MONTHS(trunc(DATE_CURRENT,'Q'), - 7)), - 3)+1 AS Q4L                     ,LAST_DAY(ADD_MONTHS(trunc(DATE_CURRENT,'Q'), - 10))AS Q5F      ,ADD_MONTHS(LAST_DAY(ADD_MONTHS(trunc(DATE_CURRENT,'Q'), - 10)), - 3)+1 AS Q5L                     ,LAST_DAY(ADD_MONTHS(trunc(DATE_CURRENT,'Q'), - 13))AS Q6F      ,ADD_MONTHS(LAST_DAY(ADD_MONTHS(trunc(DATE_CURRENT,'Q'), - 13)), - 3)+1 AS Q6L                        ,LAST_DAY(ADD_MONTHS(trunc(DATE_CURRENT,'Q'), - 16))AS Q7F      ,ADD_MONTHS(LAST_DAY(ADD_MONTHS(trunc(DATE_CURRENT,'Q'), - 16)), - 3)+1 AS Q7L                  ,LAST_DAY(ADD_MONTHS(trunc(DATE_CURRENT,'Q'), - 19))AS Q8F      ,ADD_MONTHS(LAST_DAY(ADD_MONTHS(trunc(DATE_CURRENT,'Q'), - 19)), - 3)+1 AS Q8L 从        (              SELECT TO_DATE('05 .03.2017','DD.MM.YYYY')AS DATE_CURRENT FROM DUAL        UNION SELECT TO_DATE('30 .06.2014','DD.MM.YYYY')AS DATE_CURRENT FROM DUAL        UNION SELECT TO_DATE('30 .04.2014','DD.MM.YYYY')AS DATE_CURRENT FROM DUAL        UNION SELECT TO_DATE('31 .03.2014','DD.MM.YYYY')AS DATE_CURRENT FROM DUAL        )

答案 7 :(得分:0)

我知道这个话题很老而且很封闭,但这就是我正确解决问题的方式。

SELECT TRUNC(to_date('05.06.2020', 'dd.mm.yyyy'), 'Q') f_date,
      LAST_DAY(ADD_MONTHS(TRUNC(TO_DATE('05.06.2020', 'dd.mm.yyyy'), 'Q'),2) ) l_date
 FROM dual;