数据:
Name Desc
Daman XXXX
Sam XXXX
Ram XXXX
Sun XXXX
输出:
Name Desc
Sun XXXX
Ram XXXX
没有标识栏。到目前为止,我已经尝试过以下但没有帮助
select *, Rank() over(order by Name ) as [Rank] from testdaman
答案 0 :(得分:2)
order by (select null)
实际上并未对您的记录进行排序,因为row_number()
需要over
和order by
子句,因此会使用它。订单无法保证,但您可能会发现订单与插入记录的顺序相同。
select top 2 *
from
(
select *, row_number() over (order by (select null)) [row]
from testdaman
) T
order by [row] desc
原始答案 - 需要SQL Server 2012及更高版本
select *
from testdaman
order by (select null) -- No order applied, but offset requires the order by line
offset (select count(*)-2 from testdaman) rows fetch next (2) rows only