Skip to content
Advertisement

UNION two tables with conditions from the first

I have a union on the same table [MyTable] so I can select certain values as the top 5, the logic of which I’m excluding here to simplify the question (I hope):

The first table I alias as tbl1 – can I reference this alias somehow after the UNION statement so that I can exclude the results from it?

I tried like so but it doesn’t recognise tbl1

Or do I just have to redo the first query in the 2nd half in order to get those Id’s to exclude?

I’m using SQL Server 2012 for this.

Advertisement

Answer

Use a CTE:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement