下面是输出数据、日期和时间的查询
SELECT a.CurrDate,a.item,
WeekStartingSunday = dateadd(dd,(datediff(dd,-53684,a.CurrDate)/7)*7,-53684),
WeekEndingSaturday = dateadd(dd,((datediff(dd,-53684,a.CurrDate)/7)*7)+6,-53684)
FROM
(
SELECT item = 'abc', CurrDate = getdate()
UNION ALL
SELECT item = 'def', CurrDate = getdate() +1
UNION ALL
SELECT item = 'abc', CurrDate = getdate() +2
UNION ALL
SELECT item = 'abc', CurrDate = getdate() +3
UNION ALL
SELECT item = '987', CurrDate = getdate() +4
UNION ALL
SELECT item = 'abc', CurrDate = getdate() +5
UNION ALL
SELECT item = 'abc', CurrDate = getdate() +6
UNION ALL
SELECT item = 'abc', CurrDate = getdate() +7
) a
ORDER BY a.CurrDate
结果:
CurrDate item WeekStartingSunday WeekEndingSaturday
------------------------------------------------------------------------------
2013-05-31 14:14:26.613 abc 2013-05-26 2013-06-01
2013-06-01 14:14:26.613 def 2013-05-26 2013-06-01
2013-06-02 14:14:26.613 abc 2013-06-02 2013-06-08
2013-06-03 14:14:26.613 abc 2013-06-02 2013-06-08
2013-06-04 14:14:26.613 987 2013-06-02 2013-06-08
2013-06-05 14:14:26.613 abc 2013-06-02 2013-06-08
2013-06-06 14:14:26.613 abc 2013-06-02 2013-06-08
2013-06-07 14:14:26.613 abc 2013-06-02 2013-06-08
下表是所需的结果,我想将结果输出到如下表中:
2013-05-26 to 2013-06-01
---------------------------
2013-05-31 abc
2013-06-01 def
2013-06-02 to 2013-06-08
---------------------------
2013-06-02 abc
2013-06-03 abc
2013-06-04 987
2013-06-05 abc
2013-06-06 abc
2013-06-07 abc
我很困惑,如何做到这一点?这是针对 SQL SERVER 2005 的。