sql server - How to get last 5 rows using CTE? -


i have problem using cte.

with cte  (     select          row_number() on (partition #temptable.id order #temptable.id) 'rowno',         #temptable.id,         #temptable.userid,         #temptable.code,         #temptable.codedesc     #temptable ) select distinct id, userid,code,codedesc           cte rowno <=5 

from query, want last 5 rows. getting first 5 rows. please give me solution in this.

add descending (desc) order row_number's order by clause, default ascending (asc) that's why getting first 5 rows, try in following:

with cte  (     select          row_number() on (partition #temptable.id order #temptable.actionid desc) 'rowno',         #temptable.id,         #temptable.userid,         #temptable.code,         #temptable.codedesc     #temptable ) select distinct rowno,id, userid,code,codedesc           cte rowno <=5 

Comments

Popular posts from this blog

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -

javascript - Using jquery append to add option values into a select element not working -

javascript - Restarting Supervisor and effect on FlaskSocketIO -