troubleshooting Question

Union - TOP and Bottom

Avatar of mpdillon
mpdillon asked on
Microsoft SQL Server
2 Comments1 Solution394 ViewsLast Modified:
The select statements:
Select Top(200) ID, ColA - ColB From Table
Order by ColA-ColB Asc
 and
Select Top(200) ID, ColA - ColB From Table
Order by ColA-ColB Desc

yield the correct results for the top and bottom 200 records.

I would like to combine the two queries into a single query. I have tried to use a Union.
Select Top(200) ID, ColA - ColB From Table
Order by ColA-ColB Asc
UNION
Select Top(200) ID, ColA - ColB From Table
Order by ColA-ColB Desc
but the UNION fails because of the Order By clauses.

How should I query the top 200 and bottom 200 records in a table with just one query?

Thanks,
pat
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros