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

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

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -