Reputation: 824
I have a table with a date type column among other things, and I'm trying to query the table for items based on a given date parameter, and return 10 items that fall before and 10 items that fall after the date parameter given.
Sorting on the date column in descending order and selecting 10 items that are less than or equal (<=) will accomplish half of the objective, but is there a way that I can accomplish 10 items before and 10 items after in a single query?
Upvotes: 0
Views: 6462
Reputation: 1033
select * from
(
select top 10 *
from a_table
where a_date < '2010-01-01'
order by a_date desc
union
select top 10 *
from a_table
where a_date >= '2010-01-01'
order by a_date asc
) t
order by t.a_date
Upvotes: 3