File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -135,3 +135,34 @@ FROM
135135
136136
137137
138+ -- 10.Show the username of each user along with the number
139+ -- of photos they have posted and the number of photos posted by the user
140+ -- before them and after them, based on the creation date.
141+
142+ USE ig_clone;
143+ WITH UserPhotoCounts AS (
144+ SELECT
145+ u .id ,
146+ u .username ,
147+ COUNT (p .id ) photo_count,
148+ MIN (p .created_at ) first_photo_date
149+
150+ FROM users u
151+ LEFT JOIN photos p ON u .id = p .user_id
152+ GROUP BY u .id , u .username
153+ ),
154+ UserPhotoCountsPrevNext AS (
155+ SELECT
156+ upc.* ,
157+ ROW_NUMBER() OVER (ORDER BY upc .first_photo_date ) AS rw_number
158+ FROM UserPhotoCounts upc
159+ )
160+
161+ SELECT
162+ upc .username ,
163+ upc .photo_count ,
164+ LAG(upc .photo_count ) OVER (ORDER BY upc .rw_number ) AS prev_photo_count,
165+ LEAD(upc .photo_count ) OVER (ORDER BY upc .rw_number ) AS next_photo_count
166+ FROM UserPhotoCountsPrevNext upc
167+ ORDER BY upc .rw_number ;
168+
You can’t perform that action at this time.
0 commit comments