Hi,I have a query that gets ordered by a new column created as part of a sub-query. It's now become necessary that I add a Row_Number() to the query so that I can do a LIMIT style clause (i.e. get records between 5000 and 10000). But the result is a horrible mess, and involves repeating the sub-query twice:select ROW_NUMBER() over (order by (select count(intItem1RowId) as tr from [Table1] r where r.intItem2RowId = m.intItem2RowId and r.strRelName = 'dt-na') , intWeight desc ) as rn, m.[intItem1RowId], m.[intItem2RowId],(select count(intItem1RowId) as tracks from [Table1] r where r.intItem2RowId = m.intItem2RowId and r.strRelName = 'dt-na') as trFROM [Table1] mWHERE m.[strRelName] = 'da-na' order by tracks, intWeight desc
Is there a neater way to render this code?Also, when I add this clause at the bottom:and rn > 500
I'm told that 'rn' is an invalid column name. What gives there?Cheers,Matt