How to select Previous and next event from table based on date
Hi i have a table of Showings(shows), I want to be able to select the previous and next "Show" from today (getdate()))
The table structure has this; SHOW_ID, ShowNumber, Name, EventTime
SELECT SHOW_ID, ShowNumber, Name, EventTime
FROM Event Where EventID = @EventID
Thats where i am stuck, how do i go about doing this, thanks in advance.开发者_如何学Go
-- Get the next showing of the event that will occur directly after the current datetime
SELECT TOP 1 SHOW_ID, ShowNumber, Name, EventTime
FROM Event WHERE EventTime > GetDate() AND EventId = @EventId
ORDER BY EventTime asc
-- Optionally, if you wanted to get the above and below results in a single SELECT,
-- you could use a UNION here. i.e.:
-- UNION
-- Get the first event that occurred directly before the current datetime
SELECT TOP 1 SHOW_ID, ShowNumber, Name, EventTime
FROM Event WHERE EventTime < GetDate() AND EventId = @EventId
ORDER BY EventTime desc
精彩评论