自动更新用户定义报告中的时间戳值(SQL Developer)

时间:2013-12-12 16:56:34

标签: sql oracle oracle-sqldeveloper

我已在Oracle SQL Developer中定义了一个用户定义的报告,并且每月运行以下查询:

SELECT TO_CHAR(SUMMARY_TIMESTAMP,'YYYY-MM-DD') AS day, Count(ID) AS row_count
FROM S_SEARCH_SUMMARIES 
WHERE SUMMARY_TIMESTAMP > '01-Nov-13' AND SUMMARY_TIMESTAMP < '01-Dec-13' 
AND SOURCE_INSTITUTION = 'My Institution' 
GROUP BY TO_CHAR(SUMMARY_TIMESTAMP,'YYYY-MM-DD')
ORDER BY day DESC

有没有办法根据当前月份自动更新SUMMARY_TIMESTAMP,而不是手动更改?

1 个答案:

答案 0 :(得分:1)

也许有更简洁的方法可以做到这一点,但有一种想法是在查询中添加这样的内容:

    WITH dates
     AS (SELECT TO_DATE (
                   EXTRACT (YEAR FROM SYSDATE) || '-' || EXTRACT (MONTH FROM
                   SYSDATE) || '-01',
                   'YYYY-MM-DD')
                   DT_END,
                TO_DATE ( (EXTRACT (YEAR FROM ADD_MONTHS (SYSDATE, -1))) ||
                         '-' || EXTRACT (MONTH FROM ADD_MONTHS (SYSDATE, -1))
                         || '-01',
                         'YY-MM-DD')
                   DT_START
           FROM DUAL)
  SELECT TO_CHAR (SUMMARY_TIMESTAMP, 'YYYY-MM-DD') AS day,
         COUNT (ID) AS row_count
    FROM S_SEARCH_SUMMARIES, dates
   WHERE     SUMMARY_TIMESTAMP > dates.DT_START
         AND SUMMARY_TIMESTAMP < dates.DT_END
         AND SOURCE_INSTITUTION = 'My Institution'
GROUP BY TO_CHAR (SUMMARY_TIMESTAMP, 'YYYY-MM-DD')
ORDER BY day DESC