在Redshift上按周分组

时间:2017-01-03 15:33:25

标签: sql amazon-redshift

我试图按周计算我的表,但DATE_TRUNCT('week',date)函数将星期一视为本周的开始,但我需要在星期日开始这一周。

这是一个正常运行的查询,但是从星期一开始......

SELECT DATE_TRUNC('week',myDate) AS Reference,
       column1 AS Item1,
       column2 AS Item2,
       COUNT(*) AS Volume,
       COUNT(CASE WHEN status = 'status1' THEN 1 END) AS Status1,
       COUNT(CASE WHEN status = 'status2' THEN 1 END) AS Status2,
       COUNT(CASE WHEN status = 'status2' AND fase = '1' THEN 1 END) AS Fase1,
       COUNT(CASE WHEN status = 'status2' AND fase = '2' THEN 1 END) AS Fase2,
       COUNT(CASE WHEN status = 'status2' AND fase = '3' THEN 1 END) AS Fase3
FROM myTable
WHERE DATE_TRUNC('week',myDate) = DATE_TRUNC('week',TO_DATE('12/25/2016 00:00:00','MM/dd/yyyy'))
GROUP BY 1,
         2,
         3;

到目前为止,我只尝试了另一个甚至不运行的查询,我不知道为什么,它只是说“语法错误在或接近”整数“:

SELECT DATE_TRUNC('week',myDate) - integer '1' AS Reference,
       column1 AS Item1,
       column2 AS Item2,
       COUNT(*) AS Item3,
       COUNT(CASE WHEN status = 'status1' THEN 1 END) AS Status1,
       COUNT(CASE WHEN status = 'status2' THEN 1 END) AS Status2,
       COUNT(CASE WHEN status = 'status2' AND fase = '1' THEN 1 END) AS Fase1,
       COUNT(CASE WHEN status = 'status2' AND fase = '2' THEN 1 END) AS Fase2,
       COUNT(CASE WHEN status = 'status2' AND fase = '3' THEN 1 END) AS Fase3
FROM myTable
WHERE myDate between ( DATE_TRUNC('week', TO_DATE('12/25/2016 00:00:00','MM/dd/yyyy' ) - integer '1' ) and ( DATE_TRUNC('week', TO_DATE('12/25/2016 00:00:00','MM/dd/yyyy' ) ) + integer '5' )
GROUP BY 1,
         2,
         3;

此外,即使此查询有效运行,它也会显示18 / Dec / 24 / Dec周的计数,而不是25 / Dec的25周/ 12月 - 31日/ 12月。如果他们是星期天,其他日子也会发生同样的情况。

编辑: 我刚在这个博客中找到了解决方案: https://blog.modeanalytics.com/date-trunc-sql-timestamp-function-count-on/  它引入了date_trunct函数,有人在评论中提出了同样的问题。这是我已解决的查询,以便将来参考其他人:

SELECT date_trunc('WEEK',(myDate + interval '1 day'))- interval '1 day' AS Reference
       column1 AS Item1,
       column2 AS Item2,
       COUNT(*) AS Volume,
       COUNT(CASE WHEN status = 'status1' THEN 1 END) AS Status1,
       COUNT(CASE WHEN status = 'status2' THEN 1 END) AS Status2,
       COUNT(CASE WHEN status = 'status2' AND fase = '1' THEN 1 END) AS Fase1,
       COUNT(CASE WHEN status = 'status2' AND fase = '2' THEN 1 END) AS Fase2,
       COUNT(CASE WHEN status = 'status2' AND fase = '3' THEN 1 END) AS Fase3
FROM myTable
WHERE  ( date_trunc('WEEK',(myDate + interval '1 day'))- interval '1 day') = ( DATE_TRUNC('week',TO_DATE('12/24/2016 00:00:00','MM/dd/yyyy') + interval '1 day'  ) - interval '1 day' )
GROUP BY 1,
         2,
         3;

1 个答案:

答案 0 :(得分:3)

我找不到任何简单的方法将周定为星期日到星期六。但你可以试试这个:

Hash: d437a155a1da4cdfeeeb
Version: webpack 1.14.0
Time: 77ms
    Asset     Size  Chunks             Chunk Names
bundle.js  1.51 kB       0  [emitted]  main
   [0] multi main 28 bytes {0} [built] [1 error]
    + 1 hidden modules

ERROR in The node API for `babel` has been moved to `babel-core`.
 @ multi main

这里的诀窍就是在做select date_trunc('week', myDate + 1) - 1 as Reference, ... from myTable where ... group by date_trunc('week', myDate + 1), ... 时转移一天。