I’ve tried to find an answer to my problem but couldn’t find similar example.
I have results from such a query
SELECT * FROM (
SELECT id FROM table
) AS t1
Now I would like to join t1 to another instance of itself because I need to shift it. For example if I wanted to compare a row with the previous one. I tried:
SELECT * FROM (
SELECT id FROM table
) AS t1
LEFT JOIN t1 AS t2 ON (my conditions)
But I get an error that t1 is invalid object name. When I copy my select statement:
SELECT * FROM (
SELECT id FROM table
) AS t1
LEFT JOIN (
SELECT id FROM table
) AS t2 ON (my conditions)
The above works, but is it not slower than joining to already returned results?
Any help would be appreciated
The first one is in correct:
Because you can’t alias an alias. You can do something similar to it using CTE like so: