1
我沒能獲得在weekwise算IE 7日內發貨訂單爲一週,請查看我的查詢,如下所示:如何獲得最近5周從用戶輸入的日期數據的
Declare @CurrentDate nvarchar(50);
SET @CurrentDate='2012-07-25'
BEGIN
;WITH DateList AS
(
SELECT DATEADD(WEEK, 0, CONVERT(DATETIME, @CurrentDate)) AS CreateDateTime, 1 AS Cnter
UNION ALL
SELECT DATEADD(WEEK, -1, CreateDateTime), DateList.Cnter + 1
FROM DateList
WHERE DateList.Cnter < 5
)
SELECT DateList.CreateDateTime AS ShipWeek, COALESCE(Temp.TotalCount, 0) AS TotalCount
FROM DateList
LEFT JOIN
(
SELECT COUNT(Id) TotalCount
,DATEADD(WEEK, DATEDIFF(WEEK, '19000101',CreatedDateTime),'19000101') AS ShipWeek
FROM ShipmentDetail
WHERE CreatedDateTime
BETWEEN DATEADD(DAY,-30,@CurrentDate) AND @CurrentDate
GROUP BY DATEADD(WEEK, DATEDIFF(WEEK, '19000101',CreatedDateTime),'19000101')
) Temp
ON CONVERT(VARCHAR(10), DateList.CreateDateTime, 112) = Temp.ShipWeek
END
結果表如下:
ShipWeek TotalCount
2012-07-25 00:00:00.000 0
2012-07-18 00:00:00.000 0
2012-07-11 00:00:00.000 0
2012-07-04 00:00:00.000 0
2012-06-27 00:00:00.000 0
請給我確切的解決方案。
如果我輸入當前日期,如:
SET @CurrentDate='2012-07-30'
那麼結果表會給喜歡數據:
ShipWeek TotalCount
2012-07-30 00:00:00.000 0
2012-07-23 00:00:00.000 1406
2012-07-16 00:00:00.000 0
2012-07-09 00:00:00.000 0
2012-07-02 00:00:00.000 0
爲什麼我如果我輸入當前日期像'2012-07-27'
+1 Nice ans幫了我:) – RajeshKdev 2014-04-24 10:21:53