sql - Can you reuse the alias of a derived table inside the query that makes up the derived table? -
i have seen few queries alias of derived table used in query makes derived table. can confirm if allowable or not?
here sample query. pay attention how alias "st" used twice:
select ft.thiscolumn, st.otherid firsttable ft inner join (select st.commonid,st.otherid,dateentered,dateexited,row_number() on (partition otherid order dateentered desc) strank secondtable st (@startdate between dateentered , dateexited) ) st on ft.commonid=st.commonid , st.strank=1
is ok use same alias "st" in these 2 different places?
the st inside derived table accessible inside query , inside phases executed after clause, , ok not accessible in outside context.
the second st alias whole derived table's results used in outer context , inside phases executed after clause , ok too.
as know, first of outer query clause executed , cause derived table executed , after result(which relational) returned derived table st alias , participated in join query.
additional note: please keep in mind sql server databases has close relation mathematical relations , sets, , know sets in mathematical theories should have valid name need refer them, every relation in sql server(table, view, table expression such derived table or cte , etc) should have valid name too.
but advice not use 2 aliases same name in 1 query, if logical processing phase different, because reduce readability of query.
in short, query correct , valid.