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
UNION ALL
SELECT TOP 1 Id,Name FROM Locations ORDER BY Id DESC
Any help?
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
UNION ALL
SELECT TOP 1 Id,Name FROM Locations ORDER BY Id DESC
Any help?
If you're working on SQL Server 2005 or later:
The only place this won't be like your original query is if there's only one row in the table (in which case my answer returns one row, whereas yours would return the same row twice)
Put your
order by
andtop
statements into sub-queries: