1

我想写一个查询,它给出从昨天开始的最后 14 天的 DATES 列。

例子:

Dates 

2012-06-21
2012-06-20
2012-06-19
--
-
4

2 回答 2

4
;WITH n(n) AS 
(
  SELECT TOP (14) ROW_NUMBER() OVER (ORDER BY [object_id]) 
  FROM sys.objects ORDER BY [object_id]
)
SELECT Dates = DATEADD(DAY, -n, DATEDIFF(DAY, 0, GETDATE())) FROM n
ORDER BY n;
于 2012-06-22T14:56:41.877 回答
0

其他方式;

;with days(day) as
(
    select getdate() - 1 as day
        union all
    select day - 1
        from days
        where day > dateadd(day, -14, getdate())
)
select cast(day as date) from days
于 2012-06-22T15:01:27.863 回答