1

我们有一个FixturesjQuery 轮播滚轮,它显示已完成和即将到来的装置。请参阅新闻滚轮上方的http://www.allblacks.com/ 。目前这是硬编码的。

我们希望最多可滚动 5 条记录。滚轮应显示最近完成的夹具,前向箭头显示接下来的两个即将进行的夹具,后向箭头显示最近完成的两个夹具。这通过roller js自动发生,即如果返回5条记录,则roller将显示中央记录,即记录#3。

那么,如何编写查询以提取五条记录,其中第三条记录是最近完成的夹具?

下面是数据库结构。夹具的完成由 决定eventDateTime

事件

eventID   Event                      eventDateTime
-------------------------------------------------
2016      Australia v All Blacks     2012-12-11 22:00:00.000
2015      South Africa v Australia   2012-12-04 03:00:00.000
2014      South Africa v Australia   2012-11-28 03:00:00.000
2013      South Africa v All Blacks  2012-11-22 03:00:00.000
2012      All Blacks v Australia     2012-11-07 19:35:00.000
2011      Australia v All Blacks     2012-10-31 22:00:00.000
2010      Australia v South Africa   2012-10-24 22:00:00.000
....

编辑

因此,由于eventID2012 年是截至当前日期(2012 年 11 月 18 日)最近完成的比赛,我希望返回的记录如下,eventID2012 年在中间,即第三条记录:

eventID   Event                      eventDateTime
-------------------------------------------------
2010      Australia v South Africa   2012-10-24 22:00:00.000  
2011      Australia v All Blacks     2012-10-31 22:00:00.000  
2012      All Blacks v Australia     2012-11-07 19:35:00.000
2013      South Africa v All Blacks  2012-11-22 03:00:00.000
2014      South Africa v Australia   2012-11-28 03:00:00.000
4

2 回答 2

1

You probably need just this:

select eventID, Event, eventDateTime
from
  (select top 3 *
   from events
   where DATEADD(dd, DATEDIFF(dd,0,eventDateTime), 0) <= getdate()
   order by eventDateTime) first
union all
select eventID, Event, eventDateTime
from
  (select top 2 *
   from events
   where DATEADD(dd, DATEDIFF(dd,0,eventDateTime), 0) > getdate()
   order by eventDateTime) second
于 2012-11-18T10:53:26.950 回答
0
Select * from
(
select top 3 * from #event where eventDateTime<=(getdate()) order by eventDateTime desc
UNION
select top 2 * from #event where eventDateTime>(getdate())
) a
order by eventDateTime

一个补充可能是

Select * from
(
select top 3 *,1 - row_number() over (order by GetDate() - eventDateTime)  as Sequence from #event where eventDateTime<=(getdate()) order by eventDateTime desc
UNION
select top 2 *, row_number() over (order by eventDateTime - GetDate() ) from #event where eventDateTime>(getdate())
) a
order by  eventDateTime
于 2012-11-18T10:38:19.170 回答