Building And Organizing Complex Queries | Dataquest
WITH playlist_info AS ( SELECT p.playlist_id, p.name AS playlist_name, COUNT(pt.track_id) AS number_of_tracks, SUM(t.milliseconds)/1000 AS length_seconds FROM playlist AS p LEFT JOIN playlist_track AS pt ON pt.playlist_id = p.playlist_id LEFT JOIN track AS t ON t.track_id = pt.track_id GROUP BY p.playlist_id ORDER BY p.playlist_id ) SELECT * FROM playlist_info
What I expected to happen:
I expected the returned table to properly sum up the seconds of each playlist.
What actually happened:
The length_total column was marginally off by less than 100 seconds for all playlists that had more than one track as you can see I performed the GROUP BY and all aggregations within the subquery. I am not entirely sure why this is the wrong approach and am very confused as to why my output is so similar to the solution yet just slightly off.
Replace this line with the output/error