Skip to content

Commit abdb21d

Browse files
committed
Upload 030
1 parent 8485053 commit abdb21d

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ ALL CONTENTS IN THIS REPO ARE FOR EDUCATIONAL PURPOSES ONLY.
7070
| 027 | [Laptop vs. Mobile Viewership](https://datalemur.com/questions/laptop-mobile-viewership) | [Solution](solutions/027_laptop_vs_mobile_viewership.md) | DataLemur | Easy | |
7171
| 028 | [Spotify Streaming History](https://datalemur.com/questions/spotify-streaming-history) | [Solution](solutions/028_spotify_streaming_history.md) | DataLemur | Medium | |
7272
| 029 | [Consecutive Filing Years](https://datalemur.com/questions/consecutive-filing-years) | [Solution](solutions/029_consecutive_filing_years.md) | DataLemur | Hard | |
73+
| 030 | [Average Post Hiatus (Part 1)](https://datalemur.com/questions/sql-average-post-hiatus-1) | [Solution](solutions/030_average_post_hiatus_part_1.md) | DataLemur | Easy | |
7374

7475
## Authors
7576

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# SQL Everyday \#030
2+
3+
## Average Post Hiatus (Part 1)
4+
5+
Site: DataLemur\
6+
Difficulty per Site: Easy
7+
8+
## Problem
9+
10+
Given a table of Facebook posts, for each user who posted at least twice in 2021, write a query to find the number of days between each user’s first post of the year and last post of the year in the year 2021. Output the user and number of the days between each user's first and last post. [[Full Description](https://datalemur.com/questions/sql-average-post-hiatus-1)]
11+
12+
## Submitted Solution
13+
14+
```sql
15+
-- Submitted Solution
16+
WITH cte AS (
17+
SELECT
18+
user_id
19+
,MIN(post_date) AS date_min
20+
,MAX(post_date) AS date_max
21+
FROM posts
22+
WHERE post_date BETWEEN '01/01/2021' AND '01/01/2022'
23+
GROUP BY user_id
24+
)
25+
SELECT
26+
user_id
27+
,EXTRACT(DAY FROM date_max - date_min) AS days_between
28+
FROM cte
29+
WHERE date_min < date_max
30+
;
31+
```
32+
33+
## Site Solution
34+
35+
```sql
36+
-- DataLemur Solution
37+
SELECT
38+
user_id,
39+
MAX(post_date::DATE) - MIN(post_date::DATE) AS days_between
40+
FROM posts
41+
WHERE DATE_PART('year', post_date::DATE) = 2021
42+
GROUP BY user_id
43+
HAVING COUNT(post_id)>1;
44+
```
45+
46+
## Notes
47+
48+
TODO
49+
50+
Go to [Table of Contents](/README.md#contents)\
51+
Go to [Overview](/README.md)

0 commit comments

Comments
 (0)