-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_purchase_platform.sql
More file actions
47 lines (39 loc) · 1.28 KB
/
Copy pathuser_purchase_platform.sql
File metadata and controls
47 lines (39 loc) · 1.28 KB
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
37
38
39
40
41
42
43
44
45
46
47
-- https://leetcode.com/problems/user-purchase-platform/description/
CREATE TABLE Spending (
user_id INT,
spend_date DATE,
platform VARCHAR(10),
amount INT
);
INSERT INTO Spending (user_id, spend_date, platform, amount) VALUES
(1, '2019-07-01', 'mobile', 100),
(1, '2019-07-01', 'desktop', 100),
(2, '2019-07-01', 'mobile', 100),
(2, '2019-07-02', 'mobile', 100),
(3, '2019-07-01', 'desktop', 100),
(3, '2019-07-02', 'desktop', 100);
-- SELECT spend_date, user_id,
-- SUM(amount) AS total_amount
-- FROM Spending
-- where platform in ('desktop', 'mobile')
-- GROUP BY spend_date, user_id
with cte as
(
select distinct spend_date, 'desktop' as platform from spending
union
select distinct spend_date, 'mobile' as platform from spending
union
select distinct spend_date, 'both' as platform from spending
),
cte2 as(
select spend_date, user_id
, case WHEN COUNT(DISTINCT platform) = 1 THEN MAX(platform)
when count( distinct platform) = 2 then 'both' end as plat,
sum(amount) amt
from Spending
group by spend_date, user_id)
select c.spend_date, c.platform, sum(coalesce(amt,0)) total_amount, count(distinct user_id) total_users
from cte c left join cte2 c2
on c.spend_date = c2.spend_date
and c.platform = c2.plat
group by spend_date, platform