开发者

How to select Previous and next event from table based on date

开发者 https://www.devze.com 2023-03-23 20:10 出处:网络
Hi i havea table of Showings(shows), I want to be able to select the previous and next \"Show\" from today (getdate()))

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
0

精彩评论

暂无评论...
验证码 换一张
取 消