Paging SQL Server results -
i have 2 tables, orders
, articles
. want both orders
, articles
in same result set, sorted [datecreated]
. need paging, using with. problem query below, [rownumber]
separate counter each of queries.
instead of getting 10 rows in following query, 20. ideas on how tackle this?
i need counter encompasses whole clause.
query:
;with myselectedrows ( select row_number() on (order [datecreated] desc) [rownumber] , [articleid] [id] , [datecreated] [date] , [articletext] [text] , 'articles' [rowtype] articles union select row_number() on (order [datecreated] desc) [rownumber] , [orderid] [id] , [datecreated] [date] , [ordertext] [text] , 'orders' [rowtype] orders ) select [id], [date], [text], [rowtype] myselectedrows [rownumber] between 1 , 10
you need row number on combined result set, not on each of 2 queries. therefore, can wrap union in cte/subquery, apply row_number
, paging, so:
;with cte ( select , [articleid] [id] , [datecreated] [date] , [articletext] [text] , 'articles' [rowtype] articles union select , [orderid] [id] , [datecreated] [date] , [ordertext] [text] , 'orders' [rowtype] orders ), ordered ( select * ,row_number() on (order [date] desc) [rownumber] cte ) select * ordered rownumber between 1 , 10
Comments
Post a Comment