select first.Id, first.Name
from (
select top 1 *
from Locations
order by Id) first
union all
select last.Id, last.Name
from (
select top 1 *
from Locations
order by Id desc) last
; WITH NumberedRows as (
SELECT Id,Name,
ROW_NUMBER() OVER (ORDER BY Id) as rnAsc,
ROW_NUMBER() OVER (ORDER BY Id desc) as rnDesc
FROM
Locations
)
select * from NumberedRows where rnAsc = 1 or rnDesc = 1
4条答案
按热度按时间vdgimpew1#
把你的
order by
以及top
语句到子查询:jtoj6r0c2#
ctrmrzij3#
如果您使用的是sql server 2005或更高版本:
唯一不同于原始查询的地方是如果表中只有一行(在这种情况下,我的答案返回一行,而你的答案将返回同一行两次)
zc0qhyus4#