I have a query that I am trying to assign an alias to so that I can take a primary key that I made by concatenating some of the columns together against two other queries. The code goes like the below.
SELECT Table1.Col1, Table1.Col2, Table2.Col3 .... Table1.Col1+Table1.Col2+Table2.Col3 AS 'NewPrimaryKey' FROM Table1 JOIN Table2 ON Table1.Col1 = Table2.Col3
Can I assign an alias to this query? Or would it be best to throw this into a Temp Table along with my other two queries and then Join across the three tables?
Advertisement
Answer
Yes, as other people suggested, you could do CTE, for example
;WITH CTE1 AS ( SELECT Table1.Col1+Table1.Col2+Table2.Col3 AS 'NewPrimaryKey' FROM Table1 JOIN Table2 ON Table1.Col1 = Table2.Col3 ) SELECT XXX FROM Yourtable LEFT JOIN CTE1 ON Yourtable.XX = CTE1.NewPrimaryKey