Screen Link:
Building And Organizing Complex Queries — The With Clause | Dataquest
My Code:
with stuff as
(SELECT
pt.playlist_id as playlist_id,
p.name as playlist_name,
t.name,
t.milliseconds *1000 as length_seconds
FROM track t
LEFT JOIN playlist_track as pt ON pt.track_id= t.track_id
LEFT JOIN playlist as p ON p.playlist_id = pt.playlist_id
)
select playlist_id, playlist_name, count(t.name) as number_of_tracks, length_seconds
from stuff
ORDER BY playlist_id ASC
What I expected to happen:
The correct output showing the desired columns
What actually happened:
(sqlite3.OperationalError) no such column: t.name
[SQL: with stuff as (SELECT pt.playlist_id as playlist_id, p.name as playlist_name, t.name, t.milliseconds *1000 as length_seconds FROM track t LEFT JOIN playlist_track as pt ON pt.track_id= t.track_id LEFT JOIN playlist as p ON p.playlist_id = pt.playlist_id ) select playlist_id, playlist_name, count(t.name) as number_of_tracks, length_seconds from stuff ORDER BY playlist_id ASC]
(Background on this error at: http://sqlalche.me/e/13/e3q8)
Why is it saying column t.name
does not exist? I am trying to call the name
column in the track
table which my alias is t
.
Looking at the screen here:
SQL Joins And Working With Complex Databases — Working With Larger Databases | Dataquest
You can clearly see that the column name
is in the t
table.
It is also pulling from the playlist table instead of track table.
WITH playlist_info AS
(
SELECT
p.playlist_id,
p.name playlist_name,
t.name track_name,
(t.milliseconds / 1000) length_seconds
FROM playlist p
LEFT JOIN playlist_track pt ON pt.playlist_id = p.playlist_id
LEFT JOIN track t ON t.track_id = pt.track_id
)```
Why is that?