Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update sql solution to lc problem: No.1142 #1797

Merged
merged 1 commit into from
Oct 13, 2023
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
feat: update sql solution to lc problem: No.1142
  • Loading branch information
thinkasany committed Oct 13, 2023
commit 80aa666e50f21c5c4f0a0a747b3100a55b1ab73b
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,23 @@ Activity 表:
WITH
T AS (
SELECT
count(DISTINCT session_id) AS sessions
COUNT(DISTINCT session_id) AS sessions
FROM Activity
WHERE activity_date <= '2019-07-27' AND datediff('2019-07-27', activity_date) < 30
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
SELECT IFNULL(ROUND(AVG(sessions), 2), 0) AS average_sessions_per_user
FROM T;
```

```sql
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) < 30;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,23 @@ Activity table:
WITH
T AS (
SELECT
count(DISTINCT session_id) AS sessions
COUNT(DISTINCT session_id) AS sessions
FROM Activity
WHERE activity_date <= '2019-07-27' AND datediff('2019-07-27', activity_date) < 30
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
SELECT IFNULL(ROUND(AVG(sessions), 2), 0) AS average_sessions_per_user
FROM T;
```

```sql
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) < 30;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
WITH
T AS (
SELECT
count(DISTINCT session_id) AS sessions
COUNT(DISTINCT session_id) AS sessions
FROM Activity
WHERE activity_date <= '2019-07-27' AND datediff('2019-07-27', activity_date) < 30
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
SELECT IFNULL(ROUND(AVG(sessions), 2), 0) AS average_sessions_per_user
FROM T;