Combining ORDER BY AND UNION in SQL Server

后端 未结 4 1067
渐次进展
渐次进展 2020-12-05 06:06

How can I get first record of a table and last record of a table in one result-set?

This Query fails

SELECT TOP 1 Id,Name FROM Locations ORDER BY Id
         


        
4条回答
  •  眼角桃花
    2020-12-05 07:02

    select * from (
    SELECT TOP 1 Id,Name FROM Locations ORDER BY Id) X
    UNION ALL
    SELECT TOP 1 Id,Name FROM Locations ORDER BY Id DESC
    

提交回复
热议问题