Advertisement
Guest User

Untitled

a guest
Aug 24th, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. WITH this_yw AS (
  2. SELECT year_week
  3. FROM calendar
  4. WHERE date = CURRENT_DATE
  5. ),
  6. all_yw AS (
  7. SELECT DISTINCT
  8. year_week,
  9. DENSE_RANK() OVER (ORDER BY year_week ASC) AS "yw_rank"
  10. FROM calendar
  11. ),
  12. yw_ranges AS (
  13. SELECT
  14. ty_ywk.year_week AS "this_year_week",
  15. ly_ywk.year_week AS "last_year_week"
  16. FROM this_yw
  17. JOIN all_yw AS ty_rnk ON ty_rnk.year_week = this_yw.year_week
  18. JOIN all_yw AS ty_ywk ON ty_ywk.yw_rank BETWEEN ty_rnk.yw_rank - 3 AND ty_rnk.yw_rank
  19. JOIN all_yw AS ly_ywk ON ly_ywk.yw_rank = ty_ywk.yw_rank - 100
  20. )
  21. SELECT
  22. calendar.date,
  23. 'this' AS "period"
  24. FROM calendar
  25. JOIN yw_ranges ON yw_ranges.this_year_week = calendar.year_week
  26. UNION
  27. SELECT
  28. calendar.date,
  29. 'last' AS "period"
  30. FROM calendar
  31. JOIN yw_ranges ON yw_ranges.last_year_week = calendar.year_week
  32. ORDER BY 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement