17 lines
589 B
SQL
17 lines
589 B
SQL
WITH RECURSIVE calendar(day, last_day) AS (
|
|
SELECT MIN(order_date), MAX(order_date) FROM daily_category_revenue
|
|
HAVING COUNT(*) > 0
|
|
UNION ALL
|
|
SELECT date(day, '+1 day'), last_day FROM calendar WHERE day < last_day
|
|
),
|
|
daily AS (
|
|
SELECT day AS order_date, COALESCE(SUM(r.revenue_kopecks), 0) AS revenue_kopecks
|
|
FROM calendar
|
|
LEFT JOIN daily_category_revenue AS r ON r.order_date = day
|
|
GROUP BY day
|
|
)
|
|
SELECT order_date, revenue_kopecks,
|
|
revenue_kopecks - LAG(revenue_kopecks) OVER (ORDER BY order_date) AS change_kopecks
|
|
FROM daily
|
|
ORDER BY order_date;
|