forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sales-by-day-of-the-week.sql
36 lines (35 loc) · 1.06 KB
/
sales-by-day-of-the-week.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Time: O(m + n)
# Space: O(n)
SELECT a.item_category AS 'CATEGORY',
sum(CASE
WHEN weekday(b.order_date) = 0 THEN b.quantity
ELSE 0
END) AS 'MONDAY',
sum(CASE
WHEN weekday(b.order_date) = 1 THEN b.quantity
ELSE 0
END) AS 'TUESDAY',
sum(CASE
WHEN weekday(b.order_date) = 2 THEN b.quantity
ELSE 0
END) AS 'WEDNESDAY',
sum(CASE
WHEN weekday(b.order_date) = 3 THEN b.quantity
ELSE 0
END) AS 'THURSDAY',
sum(CASE
WHEN weekday(b.order_date) = 4 THEN b.quantity
ELSE 0
END) AS 'FRIDAY',
sum(CASE
WHEN weekday(b.order_date) = 5 THEN b.quantity
ELSE 0
END) AS 'SATURDAY',
sum(CASE
WHEN weekday(b.order_date) = 6 THEN b.quantity
ELSE 0
END) AS 'SUNDAY'
FROM items a
LEFT JOIN orders b ON a.item_id = b.item_id
GROUP BY a.item_category
ORDER BY a.item_category;