在Oracle中使用周间隔和参数日期对数据进行计数

时间:2018-12-12 01:21:18

标签: sql oracle reporting-services

我正在尝试从参数开始日期到结束日期间隔一周的数据进行计数。到目前为止,我得到了以下代码:

SELECT  TRUNC(HAZARD_DATE, 'WW') week, count(*) as TOTAL 
FROM T_HAZARD a
WHERE A.HAZARD_DATE BETWEEN :parStart AND :parEnd
GROUP BY TRUNC(HAZARD_DATE, 'WW')
ORDER BY WEEK ASC

结果:

    WEEK    TOTAL
1/1/2018    181
1/8/2018    289
1/15/2018   344
1/22/2018   288

我想要的结果:

WEEK    TOTAL
 1      181
 2      289
 3      344
 4      288

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

你很近。您只需要将TRUNC函数替换为TO_CHAR(格式为WW)即可。这将返回星期数,而TRUNC(..., 'WW')返回星期几的日期。

SELECT TO_CHAR(HAZARD_DATE, 'WW') week, count(*) as TOTAL 
FROM T_HAZARD a
WHERE A.HAZARD_DATE BETWEEN :parStart AND :parEnd
GROUP BY TO_CHAR(HAZARD_DATE, 'WW')
ORDER BY WEEK ASC