Skip to content

Commit

Permalink
Create user-purchase-platform.sql
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Jul 16, 2019
1 parent 3991f3e commit c9eaeaf
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions MySQL/user-purchase-platform.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Time: O(n)
# Space: O(n)

SELECT t1.spend_date,
'both' AS platform,
Sum(Ifnull(t.sum_amount, 0)) AS total_amount,
Count(t.user_id) AS total_users
FROM (SELECT spend_date,
user_id,
Sum(amount) AS sum_amount
FROM spending
GROUP BY spend_date,
user_id
HAVING Count(platform) = 2) AS t
RIGHT JOIN (SELECT DISTINCT spend_date
FROM spending) AS t1
ON t.spend_date = t1.spend_date
GROUP BY t1.spend_date
UNION
SELECT t2.spend_date,
'mobile' AS platform,
Sum(Ifnull(t.amount, 0)) AS total_amount,
Count(t.user_id) AS total_users
FROM (SELECT spend_date,
user_id,
platform,
amount
FROM spending
GROUP BY spend_date,
user_id
HAVING Count(platform) < 2) AS t
RIGHT JOIN (SELECT DISTINCT spend_date
FROM spending) AS t2
ON t.spend_date = t2.spend_date
AND t.platform = 'mobile'
GROUP BY t2.spend_date
UNION
SELECT t3.spend_date,
'desktop' AS platform,
Sum(Ifnull(t.amount, 0)) AS total_amount,
Count(t.user_id) AS total_users
FROM (SELECT spend_date,
user_id,
platform,
amount
FROM spending
GROUP BY spend_date,
user_id
HAVING Count(platform) < 2) AS t
RIGHT JOIN (SELECT DISTINCT spend_date
FROM spending) AS t3
ON t.spend_date = t3.spend_date
AND t.platform = 'desktop'
GROUP BY t3.spend_date

0 comments on commit c9eaeaf

Please sign in to comment.