Oracle SQL:检索数周的数据

时间:2015-02-07 12:54:30

标签: sql oracle

我希望将数周内的数据查询到列中。

SELECT
    MAR.AR,
    SUM(CASE
            WHEN MAR.CREATION_DAY >= TO_DATE({RUN_DATE_YYYYMMDD}, 'YYYYMMDD')-28
                    AND MAR.CREATION_DAY <= TO_DATE({RUN_DATE_YYYYMMDD}, 'YYYYMMDD')
                THEN 1
            ELSE 0
            END) AS [WEEK_N-3]
    SUM(CASE
            WHEN MAR.CREATION_DAY >= TO_DATE({RUN_DATE_YYYYMMDD}, 'YYYYMMDD')-21
                    AND MAR.CREATION_DAY <= TO_DATE({RUN_DATE_YYYYMMDD}, 'YYYYMMDD')
                THEN 1
            ELSE 0
            END) AS [WEEK_N-2]
    SUM(CASE
            WHEN MAR.CREATION_DAY >= TO_DATE({RUN_DATE_YYYYMMDD}, 'YYYYMMDD')-14
                    AND MAR.CREATION_DAY <= TO_DATE({RUN_DATE_YYYYMMDD}, 'YYYYMMDD')
                THEN 1
            ELSE 0
            END) AS [WEEK_N-1]
    SUM(CASE
            WHEN MAR.CREATION_DAY >= TO_DATE({RUN_DATE_YYYYMMDD}, 'YYYYMMDD')-7
                    AND MAR.CREATION_DAY <= TO_DATE({RUN_DATE_YYYYMMDD}, 'YYYYMMDD')
                THEN 1
            ELSE 0
            END) AS [WEEK_N]
FROM
    TABLE_DATA MAR
WHERE
    MAR.CREATION_DAY >= TO_DATE({RUN_DATE_YYYYMMDD}, 'YYYYMMDD')-28
    AND MAR.CREATION_DAY <= TO_DATE({RUN_DATE_YYYYMMDD}, 'YYYYMMDD')
GROUP BY
    MAR.AR

我正在使用{RUN_Date_YYYYMMDD}时出现SQL错误。有没有其他方法可以做到这一点?

谢谢!

1 个答案:

答案 0 :(得分:0)

不得不使用

'{RUN_Date_YYYYMMDD}'

而不是

{RUN_Date_YYYYMMDD}