Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- Section1
CREATE INDEX idx_orders_created_at ON orders (created_at,total);
SELECT SUM(total)
FROM orders
WHERE created_at BETWEEN '2020-01-01 00:00:00' AND '2020-12-31 23:59:59';

-- Section2
create index idx_orders_user on orders (user_id,created_at,total);
SELECT SUM(total)
FROM orders
WHERE created_at BETWEEN '2020-01-01 00:00:00' AND '2020-12-31 23:59:59'
AND user_id = 345;

-- Section3
WITH RECURSIVE cte AS (
SELECT MIN(DATE(created_at)) AS date
FROM orders
UNION ALL
SELECT DATE_ADD(cte.date, INTERVAL 1 DAY)
FROM cte
WHERE DATE_ADD(cte.date, INTERVAL 1 DAY) <= (SELECT MAX(created_at) FROM orders)
)
SELECT DATE(cte.date) AS date,
SUM(IFNULL(orders.total, 0)) AS total_value
FROM cte
LEFT JOIN (
SELECT DATE(created_at) AS order_date, total AS total
FROM orders
) AS orders ON cte.date = orders.order_date
GROUP BY cte.date
ORDER BY cte.date ASC;