forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |