所以这里我找到了TYWTD(今年迄今为止) 我得到了今年的查询,但我需要知道如何编写去年4-5-4日历日期的查询。
如果我今天提交报告,则应该遵循日期范围。
今年WTD日期范围应为6/11/17 - 6/13/17 去年WTD日期范围应为6/12/17 - 6/14/17
有人可以告诉我去年的WTD吗?
谢谢!
SELECT MAX(te.StoreID) AS StoreID,
SUM(te.Price * te.Quantity) AS Sales,
SUM(te.Cost * te.Quantity) AS Cost,
COUNT(DISTINCT t.TransactionNumber) AS Trxn,
SUM(te.Quantity) AS Quantity
FROM TransactionEntry te
INNER JOIN [Transaction] t
ON te.TransactionNumber = t.TransactionNumber
AND te.StoreID = t.StoreID
LEFT JOIN item i
ON te.itemID = i.ID
LEFT JOIN Department d
ON i.DepartmentID = d.ID
WHERE d.ID <> 8
AND CONVERT(date, t.Time) >= dateadd(day, 1-datepart(dw, getdate()), convert(date, getdate()))
AND CONVERT(date, t.Time) <= dateadd(day, 8-datepart(dw, getdate()), convert(date, getdate()))
GROUP BY te.StoreID) AS TYWTD
答案 0 :(得分:2)
从GetDate()
减去一年应该可以解决问题。
dateadd(day, 1-datepart(dw, DateAdd(year,-1,getdate())), convert(date, DateAdd(year,-1,getdate())))
这会产生2016-06-12
。
这是更新的脚本。 再次更新。
SELECT MAX(te.StoreID) AS StoreID,
SUM(te.Price * te.Quantity) AS Sales,
SUM(te.Cost * te.Quantity) AS Cost,
COUNT(DISTINCT t.TransactionNumber) AS Trxn,
SUM(te.Quantity) AS Quantity
FROM TransactionEntry te
INNER JOIN [Transaction] t
ON te.TransactionNumber = t.TransactionNumber
AND te.StoreID = t.StoreID
LEFT JOIN item i
ON te.itemID = i.ID
LEFT JOIN Department d
ON i.DepartmentID = d.ID
WHERE d.ID <> 8
AND CONVERT(date, t.Time) >= dateadd(day, 1-datepart(dw, DateAdd(year,-1,getdate())), convert(date, DateAdd(year,-1,getdate())))
AND dateadd(day, 8-datepart(dw, DateAdd(year,-1,getdate())), convert(date, DateAdd(year,-1,getdate())))
GROUP BY te.StoreID) AS TYWTD