-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1127.sql
38 lines (37 loc) · 969 Bytes
/
1127.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
37
38
-- [ LeetCode ] 1127. User Purchase Platform
SELECT
DateSpendings.spend_date,
DateSpendings.platform,
IFNULL(TotalReports.total_amount, 0) AS total_amount,
IFNULL(TotalReports.total_users, 0) AS total_users
FROM (
SELECT
DISTINCT Spending.spend_date,
Platforms.platform
FROM Spending
CROSS JOIN (
SELECT 'mobile' AS platform
UNION ALL
SELECT 'desktop'
UNION ALL
SELECT 'both'
) AS Platforms
) AS DateSpendings
LEFT JOIN (
SELECT
spend_date,
platform,
SUM(amount) AS total_amount,
COUNT(user_id) AS total_users
FROM (
SELECT
user_id,
spend_date,
IF(COUNT(platform) = 2, 'both', platform) AS platform,
SUM(amount) AS amount
FROM Spending
GROUP BY user_id, spend_date
) AS UserReports
GROUP BY spend_date, platform
) AS TotalReports
USING (spend_date, platform);