私は週ごとに自分のテーブルを数えようとしていますが、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;
は、これまでのところ私だけでも実行すると、私はなぜ知らないdoesntの別のクエリを適切に実行されますが、月曜日に開始すると、クエリ、試されている、それだけで「構文エラーを語りますで、または近くに「整数」: - 24/12月ではなく週25/12月 - 31/12月中にも
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;
、このクエリはpropely runnedた場合でも、それは週の数18/12月を表示します12月25日。日曜日の場合、他の日にも同じことが起こる。
編集: 私はこのブログの解決策を見つけました。 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;