fork download
  1. CREATE TABLE orders (
  2. amount INT,
  3. order_date DATE,
  4. order_id INT PRIMARY KEY,
  5. product_id VARCHAR(5)
  6. );
  7.  
  8. CREATE TABLE calendar_dim (
  9. cal_date DATE PRIMARY KEY
  10. );
  11.  
  12. INSERT INTO calendar_dim (cal_date) VALUES
  13. ('2024-01-01'),
  14. ('2024-01-02'),
  15. ('2024-01-03'),
  16. ('2024-01-04'),
  17. ('2024-01-05'),
  18. ('2024-01-06'),
  19. ('2024-01-07'),
  20. ('2024-01-08'),
  21. ('2024-01-09'),
  22. ('2024-01-10'),
  23. ('2024-01-11'),
  24. ('2024-01-12'),
  25. ('2024-01-13'),
  26. ('2024-01-14'),
  27. ('2024-01-15'),
  28. ('2024-01-16'),
  29. ('2024-01-17'),
  30. ('2024-01-18'),
  31. ('2024-01-19'),
  32. ('2024-01-20'),
  33. ('2024-01-21'),
  34. ('2024-01-22'),
  35. ('2024-01-23'),
  36. ('2024-01-24'),
  37. ('2024-01-25'),
  38. ('2024-01-26'),
  39. ('2024-01-27'),
  40. ('2024-01-28'),
  41. ('2024-01-29'),
  42. ('2024-01-30'),
  43. ('2024-01-31');
  44.  
  45. INSERT INTO orders (order_id, order_date, product_id, amount) VALUES
  46. (1, '2024-01-01', 'p1', 100),
  47. (2, '2024-01-01', 'p1', 150),
  48. (3, '2024-01-03', 'p1', 120),
  49. (4, '2024-01-04', 'p2', 200),
  50. (5, '2024-01-05', 'p1', 180),
  51. (6, '2024-01-06', 'p1', 110),
  52. (7, '2024-01-07', 'p1', 220),
  53. (8, '2024-01-08', 'p2', 130),
  54. (9, '2024-01-09', 'p1', 190),
  55. (10, '2024-01-10', 'p2', 240),
  56. (11, '2024-01-11', 'p1', 140),
  57. (12, '2024-01-12', 'p2', 200),
  58. (13, '2024-01-13', 'p2', 260),
  59. (14, '2024-01-14', 'p2', 150),
  60. (15, '2024-01-15', 'p1', 210),
  61. (16, '2024-01-16', 'p2', 280),
  62. (17, '2024-01-16', 'p2', 160),
  63. (18, '2024-01-18', 'p2', 220),
  64. (19, '2024-01-19', 'p1', 300),
  65. (20, '2024-01-20', 'p1', 170),
  66. (21, '2024-01-21', 'p1', 230),
  67. (22, '2024-01-22', 'p2', 320),
  68. (23, '2024-01-23', 'p1', 180),
  69. (24, '2024-01-24', 'p1', 240),
  70. (25, '2024-01-25', 'p1', 340),
  71. (26, '2024-01-26', 'p2', 190),
  72. (27, '2024-01-27', 'p1', 250),
  73. (28, '2024-01-28', 'p2', 360),
  74. (29, '2024-01-29', 'p1', 200),
  75. (30, '2024-01-30', 'p2', 260);
  76.  
  77. with cte as (
  78. select product_id,order_date,sum(amount) as sales
  79. from orders
  80. group by product_id,order_date
  81. )
  82. , all_products_dates as (
  83. select distinct product_id, cal_date as order_date
  84. from cte
  85. cross join calendar_dim
  86. )
  87. select a.product_id,a.order_date,coalesce(cte.sales,0) as sales
  88. ,sum(coalesce(cte.sales,0)) over(partition by a.product_id order by a.order_date rows between 2 preceding and current row) as rolling3_sum
  89. from all_products_dates a
  90. left join cte on a.product_id=cte.product_id and a.order_date=cte.order_date
  91. ORDER BY a.product_id , a.order_date;
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
p1|2024-01-01|250|250
p1|2024-01-02|0|250
p1|2024-01-03|120|370
p1|2024-01-04|0|120
p1|2024-01-05|180|300
p1|2024-01-06|110|290
p1|2024-01-07|220|510
p1|2024-01-08|0|330
p1|2024-01-09|190|410
p1|2024-01-10|0|190
p1|2024-01-11|140|330
p1|2024-01-12|0|140
p1|2024-01-13|0|140
p1|2024-01-14|0|0
p1|2024-01-15|210|210
p1|2024-01-16|0|210
p1|2024-01-17|0|210
p1|2024-01-18|0|0
p1|2024-01-19|300|300
p1|2024-01-20|170|470
p1|2024-01-21|230|700
p1|2024-01-22|0|400
p1|2024-01-23|180|410
p1|2024-01-24|240|420
p1|2024-01-25|340|760
p1|2024-01-26|0|580
p1|2024-01-27|250|590
p1|2024-01-28|0|250
p1|2024-01-29|200|450
p1|2024-01-30|0|200
p1|2024-01-31|0|200
p2|2024-01-01|0|0
p2|2024-01-02|0|0
p2|2024-01-03|0|0
p2|2024-01-04|200|200
p2|2024-01-05|0|200
p2|2024-01-06|0|200
p2|2024-01-07|0|0
p2|2024-01-08|130|130
p2|2024-01-09|0|130
p2|2024-01-10|240|370
p2|2024-01-11|0|240
p2|2024-01-12|200|440
p2|2024-01-13|260|460
p2|2024-01-14|150|610
p2|2024-01-15|0|410
p2|2024-01-16|440|590
p2|2024-01-17|0|440
p2|2024-01-18|220|660
p2|2024-01-19|0|220
p2|2024-01-20|0|220
p2|2024-01-21|0|0
p2|2024-01-22|320|320
p2|2024-01-23|0|320
p2|2024-01-24|0|320
p2|2024-01-25|0|0
p2|2024-01-26|190|190
p2|2024-01-27|0|190
p2|2024-01-28|360|550
p2|2024-01-29|0|360
p2|2024-01-30|260|620
p2|2024-01-31|0|260