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
Post a Comment