如何根据日期从表中选择上一个和下一个事件

时间:2011-07-30 17:14:57

标签: sql

您好我有一个Showings表(节目),我希望能够从今天开始选择上一个和下一个“Show”(getdate()))

表结构有这个; SHOW_ID,ShowNumber,Name,EventTime

SELECT  SHOW_ID, ShowNumber, Name, EventTime
FROM Event Where EventID = @EventID

多数民众赞成在哪里被困,我该怎么做呢,提前谢谢。

1 个答案:

答案 0 :(得分:1)

-- 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