yql-18511.sql 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* yt can not */
  2. $round_period = ($day, $period) -> {
  3. RETURN
  4. CASE
  5. WHEN $period = 'd' THEN $day
  6. WHEN $period = 'w' THEN DateTime::MakeDate(DateTime::StartOfWeek($day))
  7. WHEN $period = 'm' THEN DateTime::MakeDate(DateTime::StartOfMonth($day))
  8. ELSE $day
  9. END
  10. };
  11. $data =
  12. SELECT
  13. $round_period(day, 'd') AS day,
  14. $round_period(day, 'w') AS week,
  15. $round_period(day, 'm') AS month,
  16. IF(user_card_cnt <= 10, user_card_cnt, 11) AS user_cards_segm,
  17. is_proven_owner,
  18. user_id,
  19. FROM (
  20. SELECT
  21. Date("2024-04-29") AS day,
  22. "ALLO" AS mark,
  23. "???" AS model,
  24. 5 AS user_card_cnt,
  25. 'ACTIVE' AS status,
  26. 999 AS user_id,
  27. 1 AS is_proven_owner,
  28. UNION ALL
  29. SELECT
  30. Date("2024-04-29") AS day,
  31. "ALLO" AS mark,
  32. "!!!!!!" AS model,
  33. 50 AS user_card_cnt,
  34. 'ACTIVE' AS status,
  35. 1111 AS user_id,
  36. 0 AS is_proven_owner,
  37. );
  38. SELECT
  39. day,
  40. GROUPING(day) AS grouping_day,
  41. week,
  42. GROUPING(week) AS grouping_week,
  43. month,
  44. GROUPING(month) as grouping_month,
  45. CASE
  46. WHEN GROUPING(week) == 1 AND GROUPING(month) == 1 THEN 'd'
  47. WHEN GROUPING(day) == 1 AND GROUPING(month) == 1 THEN 'w'
  48. WHEN GROUPING(day) == 1 AND GROUPING(week) == 1 THEN 'm'
  49. ELSE NULL
  50. END AS period_type,
  51. user_cards_segm,
  52. if(GROUPING(user_cards_segm) = 1, -300, user_cards_segm) AS __user_cards_segm__,
  53. GROUPING(user_cards_segm) as grouping_user_cards_segm,
  54. COUNT(DISTINCT user_id) AS all_user_qty,
  55. FROM $data AS t
  56. GROUP BY
  57. GROUPING SETS(
  58. -- day grouping
  59. (day),
  60. (day, user_cards_segm),
  61. -- -- week grouping
  62. (week),
  63. (week, user_cards_segm),
  64. -- -- month grouping
  65. (month),
  66. (month, user_cards_segm)
  67. )