Skip to content

Commit fd08212

Browse files
authored
feat: add sql solution to lc problem: No.1127 (doocs#1090)
No.1127.User Purchase Platform
1 parent e221be8 commit fd08212

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

.prettierrc

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"solution/1000-1099/1097.Game Play Analysis V/Solution.sql",
2121
"solution/1000-1099/1098.Unpopular Books/Solution.sql",
2222
"solution/1100-1199/1113.Reported Posts/Solution.sql",
23+
"solution/1100-1199/1127.User Purchase Platform/Solution.sql",
2324
"solution/1100-1199/1174.Immediate Food Delivery II/Solution.sql",
2425
"solution/1100-1199/1193.Monthly Transactions I/Solution.sql",
2526
"solution/1200-1299/1205.Monthly Transactions II/Solution.sql",

solution/1100-1199/1127.User Purchase Platform/README.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,32 @@ Result table:
6363
### **SQL**
6464

6565
```sql
66-
66+
# Write your MySQL query statement below
67+
WITH
68+
s AS (
69+
SELECT DISTINCT spend_date, 'desktop' AS platform FROM Spending
70+
UNION
71+
SELECT DISTINCT spend_date, 'mobile' AS platform FROM Spending
72+
UNION
73+
SELECT DISTINCT spend_date, 'both' AS platform FROM Spending
74+
),
75+
t AS (
76+
SELECT
77+
user_id,
78+
spend_date,
79+
if(count(platform) = 2, 'both', platform) AS platform,
80+
sum(amount) AS amount
81+
FROM Spending
82+
GROUP BY 1, 2
83+
)
84+
SELECT
85+
t1.*,
86+
ifnull(sum(amount), 0) AS total_amount,
87+
count(t2.user_id) AS total_users
88+
FROM
89+
s AS t1
90+
LEFT JOIN t AS t2 USING (spend_date, platform)
91+
GROUP BY 1, 2;
6792
```
6893

6994
<!-- tabs:end -->

solution/1100-1199/1127.User Purchase Platform/README_EN.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,32 @@ On 2019-07-02, user 2 purchased using mobile <strong>only</strong>, user 3 purch
6767
### **SQL**
6868

6969
```sql
70-
70+
# Write your MySQL query statement below
71+
WITH
72+
s AS (
73+
SELECT DISTINCT spend_date, 'desktop' AS platform FROM Spending
74+
UNION
75+
SELECT DISTINCT spend_date, 'mobile' AS platform FROM Spending
76+
UNION
77+
SELECT DISTINCT spend_date, 'both' AS platform FROM Spending
78+
),
79+
t AS (
80+
SELECT
81+
user_id,
82+
spend_date,
83+
if(count(platform) = 2, 'both', platform) AS platform,
84+
sum(amount) AS amount
85+
FROM Spending
86+
GROUP BY 1, 2
87+
)
88+
SELECT
89+
t1.*,
90+
ifnull(sum(amount), 0) AS total_amount,
91+
count(t2.user_id) AS total_users
92+
FROM
93+
s AS t1
94+
LEFT JOIN t AS t2 USING (spend_date, platform)
95+
GROUP BY 1, 2;
7196
```
7297

7398
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
s AS (
4+
SELECT DISTINCT spend_date, 'desktop' AS platform FROM Spending
5+
UNION
6+
SELECT DISTINCT spend_date, 'mobile' AS platform FROM Spending
7+
UNION
8+
SELECT DISTINCT spend_date, 'both' AS platform FROM Spending
9+
),
10+
t AS (
11+
SELECT
12+
user_id,
13+
spend_date,
14+
if(count(platform) = 2, 'both', platform) AS platform,
15+
sum(amount) AS amount
16+
FROM Spending
17+
GROUP BY 1, 2
18+
)
19+
SELECT
20+
t1.*,
21+
ifnull(sum(amount), 0) AS total_amount,
22+
count(t2.user_id) AS total_users
23+
FROM
24+
s AS t1
25+
LEFT JOIN t AS t2 USING (spend_date, platform)
26+
GROUP BY 1, 2;

0 commit comments

Comments
 (0)