以下查询正在生成数据,但仅返回值的周数是过去四个月中的第10,11,12周。我需要能够获得过去12周的周数,即在枢轴部分使用的周数10,11,12,13,14,15,16,17,18,19,20,21,22但是公式像dateadd(周,-1,发票)不起作用
我正在使用sql server 2008 r2
感谢
SELECT productid,
[1] AS 'This Week',
[2] AS 'Last Week',
[3] AS 'Week -3',
[4] AS 'Week -4',
[5] AS 'Week -5',
[6] AS 'Week -6',
[7] AS 'Week -7',
[8] AS 'Week -8',
[9] AS 'Week -9',
[10] AS 'Week -10',
[11] AS 'Week -11',
[12] AS 'Week -12'
FROM (SELECT productid,
qtyinvoiced,
Datepart(week, invoicedate) AS Tweek
FROM dbo.invoicedetail
WHERE invoicedate > Dateadd(m, -4, CURRENT_TIMESTAMP)) source
PIVOT ( Sum(qtyinvoiced)
FOR tweek IN ( [1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9],
[10],
[11],
[12] ) ) AS pvtmonth
答案 0 :(得分:0)
而不是使用
DATEPART(week, Invoicedate) as Tweek
您可以使用
-DATEDIFF(WEEK, CURRENT_TIMESTAMP, InvoiceDate) AS Tweek
这将显示现在和发票日期之间的差异,而不是发票日期的周数