Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions solution/1100-1199/1127.User Purchase Platform/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,29 @@ Result table:
```sql
# Write your MySQL query statement below
WITH
s AS (
P AS (
SELECT DISTINCT spend_date, 'desktop' AS platform FROM Spending
UNION
SELECT DISTINCT spend_date, 'mobile' AS platform FROM Spending
SELECT DISTINCT spend_date, 'mobile' FROM Spending
UNION
SELECT DISTINCT spend_date, 'both' AS platform FROM Spending
SELECT DISTINCT spend_date, 'both' FROM Spending
),
t AS (
T AS (
SELECT
user_id,
spend_date,
if(count(platform) = 2, 'both', platform) AS platform,
sum(amount) AS amount
sum(amount) AS amount,
if(count(platform) = 1, platform, 'both') AS platform
FROM Spending
GROUP BY 1, 2
)
SELECT
t1.*,
p.*,
ifnull(sum(amount), 0) AS total_amount,
count(t2.user_id) AS total_users
count(t.user_id) AS total_users
FROM
s AS t1
LEFT JOIN t AS t2 USING (spend_date, platform)
P AS p
LEFT JOIN T AS t USING (spend_date, platform)
GROUP BY 1, 2;
```

Expand Down
20 changes: 10 additions & 10 deletions solution/1100-1199/1127.User Purchase Platform/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,29 @@ On 2019-07-02, user 2 purchased using mobile <strong>only</strong>, user 3 purch
```sql
# Write your MySQL query statement below
WITH
s AS (
P AS (
SELECT DISTINCT spend_date, 'desktop' AS platform FROM Spending
UNION
SELECT DISTINCT spend_date, 'mobile' AS platform FROM Spending
SELECT DISTINCT spend_date, 'mobile' FROM Spending
UNION
SELECT DISTINCT spend_date, 'both' AS platform FROM Spending
SELECT DISTINCT spend_date, 'both' FROM Spending
),
t AS (
T AS (
SELECT
user_id,
spend_date,
if(count(platform) = 2, 'both', platform) AS platform,
sum(amount) AS amount
sum(amount) AS amount,
if(count(platform) = 1, platform, 'both') AS platform
FROM Spending
GROUP BY 1, 2
)
SELECT
t1.*,
p.*,
ifnull(sum(amount), 0) AS total_amount,
count(t2.user_id) AS total_users
count(t.user_id) AS total_users
FROM
s AS t1
LEFT JOIN t AS t2 USING (spend_date, platform)
P AS p
LEFT JOIN T AS t USING (spend_date, platform)
GROUP BY 1, 2;
```

Expand Down
20 changes: 10 additions & 10 deletions solution/1100-1199/1127.User Purchase Platform/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# Write your MySQL query statement below
WITH
s AS (
P AS (
SELECT DISTINCT spend_date, 'desktop' AS platform FROM Spending
UNION
SELECT DISTINCT spend_date, 'mobile' AS platform FROM Spending
SELECT DISTINCT spend_date, 'mobile' FROM Spending
UNION
SELECT DISTINCT spend_date, 'both' AS platform FROM Spending
SELECT DISTINCT spend_date, 'both' FROM Spending
),
t AS (
T AS (
SELECT
user_id,
spend_date,
if(count(platform) = 2, 'both', platform) AS platform,
sum(amount) AS amount
sum(amount) AS amount,
if(count(platform) = 1, platform, 'both') AS platform
FROM Spending
GROUP BY 1, 2
)
SELECT
t1.*,
p.*,
ifnull(sum(amount), 0) AS total_amount,
count(t2.user_id) AS total_users
count(t.user_id) AS total_users
FROM
s AS t1
LEFT JOIN t AS t2 USING (spend_date, platform)
P AS p
LEFT JOIN T AS t USING (spend_date, platform)
GROUP BY 1, 2;
16 changes: 9 additions & 7 deletions solution/1100-1199/1132.Reported Posts II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,20 @@ Removals table:

```sql
# Write your MySQL query statement below
SELECT
round(avg(avg_per * 100), 2) AS average_daily_percent
FROM
(
WITH
T AS (
SELECT
count(DISTINCT t2.post_id) / count(DISTINCT t1.post_id) AS avg_per
count(DISTINCT t2.post_id) / count(
DISTINCT t1.post_id
) * 100 AS percent
FROM
Actions AS t1
LEFT JOIN Removals AS t2 ON t1.post_id = t2.post_id
WHERE t1.extra = 'spam'
WHERE extra = 'spam'
GROUP BY action_date
) AS t3;
)
SELECT round(avg(percent), 2) AS average_daily_percent
FROM T;
```

<!-- tabs:end -->
16 changes: 9 additions & 7 deletions solution/1100-1199/1132.Reported Posts II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,20 @@ Note that the output is only one number and that we do not care about the remove

```sql
# Write your MySQL query statement below
SELECT
round(avg(avg_per * 100), 2) AS average_daily_percent
FROM
(
WITH
T AS (
SELECT
count(DISTINCT t2.post_id) / count(DISTINCT t1.post_id) AS avg_per
count(DISTINCT t2.post_id) / count(
DISTINCT t1.post_id
) * 100 AS percent
FROM
Actions AS t1
LEFT JOIN Removals AS t2 ON t1.post_id = t2.post_id
WHERE t1.extra = 'spam'
WHERE extra = 'spam'
GROUP BY action_date
) AS t3;
)
SELECT round(avg(percent), 2) AS average_daily_percent
FROM T;
```

<!-- tabs:end -->
16 changes: 9 additions & 7 deletions solution/1100-1199/1132.Reported Posts II/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Write your MySQL query statement below
SELECT
round(avg(avg_per * 100), 2) AS average_daily_percent
FROM
(
WITH
T AS (
SELECT
count(DISTINCT t2.post_id) / count(DISTINCT t1.post_id) AS avg_per
count(DISTINCT t2.post_id) / count(
DISTINCT t1.post_id
) * 100 AS percent
FROM
Actions AS t1
LEFT JOIN Removals AS t2 ON t1.post_id = t2.post_id
WHERE t1.extra = 'spam'
WHERE extra = 'spam'
GROUP BY action_date
) AS t3;
)
SELECT round(avg(percent), 2) AS average_daily_percent
FROM T;
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,22 @@ Activity table:

<!-- 这里可写通用的实现逻辑 -->

**方法一:GROUP BY + HAVING**

我们查询出所有在 `2019-07-27` 且在 $30$ 天内的所有活动记录,然后按照日期分组,统计每天的去重活跃用户数。

<!-- tabs:start -->

### **SQL**

```sql
SELECT
activity_date AS day,
COUNT(DISTINCT user_id) AS active_users
# Write your MySQL query statement below
SELECT activity_date AS day, count(DISTINCT user_id) AS active_users
FROM Activity
WHERE
DATEDIFF('2019-07-27', activity_date) >= 0
AND DATEDIFF('2019-07-27', activity_date) < 30
GROUP BY activity_date;
activity_date <= '2019-07-27'
AND datediff('2019-07-27', activity_date) < 30
GROUP BY 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,13 @@ Activity table:
### **SQL**

```sql
SELECT
activity_date AS day,
COUNT(DISTINCT user_id) AS active_users
# Write your MySQL query statement below
SELECT activity_date AS day, count(DISTINCT user_id) AS active_users
FROM Activity
WHERE
DATEDIFF('2019-07-27', activity_date) >= 0
AND DATEDIFF('2019-07-27', activity_date) < 30
GROUP BY activity_date;
activity_date <= '2019-07-27'
AND datediff('2019-07-27', activity_date) < 30
GROUP BY 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
SELECT
activity_date AS day,
COUNT(DISTINCT user_id) AS active_users
# Write your MySQL query statement below
SELECT activity_date AS day, count(DISTINCT user_id) AS active_users
FROM Activity
WHERE
DATEDIFF('2019-07-27', activity_date) >= 0
AND DATEDIFF('2019-07-27', activity_date) < 30
GROUP BY activity_date;
activity_date <= '2019-07-27'
AND datediff('2019-07-27', activity_date) < 30
GROUP BY 1;
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,18 @@ Activity 表:

```sql
# Write your MySQL query statement below
SELECT
IFNULL(
ROUND(COUNT(DISTINCT session_id) / COUNT(DISTINCT user_id), 2),
0
) AS average_sessions_per_user
FROM Activity
WHERE
DATEDIFF('2019-07-27', activity_date) >= 0
AND DATEDIFF('2019-07-27', activity_date) < 30;
WITH
T AS (
SELECT
count(DISTINCT session_id) AS sessions
FROM Activity
WHERE
activity_date <= '2019-07-27'
AND datediff('2019-07-27', activity_date) < 30
GROUP BY user_id
)
SELECT ifnull(round(avg(sessions), 2), 0) AS average_sessions_per_user
FROM T;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,18 @@ Activity table:

```sql
# Write your MySQL query statement below
SELECT
IFNULL(
ROUND(COUNT(DISTINCT session_id) / COUNT(DISTINCT user_id), 2),
0
) AS average_sessions_per_user
FROM Activity
WHERE
DATEDIFF('2019-07-27', activity_date) >= 0
AND DATEDIFF('2019-07-27', activity_date) < 30;
WITH
T AS (
SELECT
count(DISTINCT session_id) AS sessions
FROM Activity
WHERE
activity_date <= '2019-07-27'
AND datediff('2019-07-27', activity_date) < 30
GROUP BY user_id
)
SELECT ifnull(round(avg(sessions), 2), 0) AS average_sessions_per_user
FROM T;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Write your MySQL query statement below
SELECT
IFNULL(
ROUND(COUNT(DISTINCT session_id) / COUNT(DISTINCT user_id), 2),
0
) AS average_sessions_per_user
FROM Activity
WHERE
DATEDIFF('2019-07-27', activity_date) >= 0
AND DATEDIFF('2019-07-27', activity_date) < 30;
WITH
T AS (
SELECT
count(DISTINCT session_id) AS sessions
FROM Activity
WHERE
activity_date <= '2019-07-27'
AND datediff('2019-07-27', activity_date) < 30
GROUP BY user_id
)
SELECT ifnull(round(avg(sessions), 2), 0) AS average_sessions_per_user
FROM T;
9 changes: 6 additions & 3 deletions solution/1100-1199/1148.Article Views I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,20 @@ Views 表:

<!-- 这里可写通用的实现逻辑 -->

“`DISTINCT` + `ORDER BY`” 实现。
**方法一:DISTINCT + WHERE**

我们利用 `WHERE` 子句来筛选出 `author_id` 和 `viewer_id` 相等的记录,然后利用 `DISTINCT` 来去重,最后按照 `id` 排序即可。

<!-- tabs:start -->

### **SQL**

```sql
SELECT DISTINCT (author_id) AS id
# Write your MySQL query statement below
SELECT DISTINCT author_id AS id
FROM Views
WHERE author_id = viewer_id
ORDER BY id;
ORDER BY 1;
```

<!-- tabs:end -->
5 changes: 3 additions & 2 deletions solution/1100-1199/1148.Article Views I/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ Views table:
### **SQL**

```sql
SELECT DISTINCT (author_id) AS id
# Write your MySQL query statement below
SELECT DISTINCT author_id AS id
FROM Views
WHERE author_id = viewer_id
ORDER BY id;
ORDER BY 1;
```

<!-- tabs:end -->
5 changes: 3 additions & 2 deletions solution/1100-1199/1148.Article Views I/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
SELECT DISTINCT (author_id) AS id
# Write your MySQL query statement below
SELECT DISTINCT author_id AS id
FROM Views
WHERE author_id = viewer_id
ORDER BY id;
ORDER BY 1;
Loading