我可以添加六天到一個日期,並得到我的期望,但如果我添加七天到同一日期,我得到相同的日期。爲什麼不同日期的數學運算產生相同的值?
我的意思是這個;當用戶輸入「2015年12月27日」作爲@BegDate參數的值,這個聲明:
DECLARE
@Week1End datetime = DATEADD(Day, 6, @BegDate),
@Week2Begin datetime = DATEADD(Day, 7, @BegDate);
...結果按@ Week1End和@ Week2Begin是相同的日期(2016 -01-02),你可以從返回的結果集看到:
這是TSQL:
CREATE PROCEDURE [dbo].[variancePriceByProductWithPriceChangePercentage]
@Unit varchar(25),
@BegDate datetime,
@EndDate datetime
AS
DECLARE
@Week1End datetime = DATEADD(Day, 6, @BegDate),
@Week2Begin datetime = DATEADD(Day, 7, @BegDate);
Select Ind.Description,
@BegDate BegDate,
@Week1End Week1End,
@Week1End Week2Begin,
@EndDate EndDate,
SUM(CASE WHEN Ind.InvoiceDate BETWEEN @BegDate AND @Week1End THEN Ind.QtyShipped ELSE 0 END)
Week1Usage,
SUM(CASE WHEN Ind.InvoiceDate BETWEEN @Week2Begin AND @EndDate THEN Ind.QtyShipped ELSE 0 END)
Week2Usage,
SUM(CASE WHEN Ind.InvoiceDate BETWEEN @BegDate AND @Week1End THEN Ind.Price ELSE 0 END)
Week1Price,
SUM(CASE WHEN Ind.InvoiceDate BETWEEN @Week2Begin AND @EndDate THEN Ind.Price ELSE 0 END) -
SUM(CASE WHEN Ind.InvoiceDate BETWEEN @BegDate AND @Week1End THEN Ind.QtyShipped ELSE 0 END)
UsageVariance,
SUM(CASE WHEN Ind.InvoiceDate BETWEEN @Week2Begin AND @EndDate THEN Ind.QtyShipped ELSE 0 END)
-
SUM(CASE WHEN Ind.InvoiceDate BETWEEN @BegDate AND @Week1End THEN Ind.Price ELSE 0 END)
PriceVariance,
(SUM(CASE WHEN Ind.InvoiceDate BETWEEN @Week2Begin AND @EndDate THEN Ind.QtyShipped ELSE 0
END) -
SUM(CASE WHEN Ind.InvoiceDate BETWEEN @BegDate AND @Week1End THEN Ind.Price ELSE 0 END))
/
NULLIF(SUM(CASE WHEN Ind.InvoiceDate BETWEEN @BegDate AND @Week1End THEN Ind.Price ELSE 0 END),0)
PercentageOfPriceVariance
From InvoiceDetail Ind
Where Ind.Unit = @Unit
AND Ind.InvoiceDate BETWEEN @BegDate AND @EndDate
Group By Ind.Description
所以爲什麼@BegDate + 6等同於同樣的事情@BegDate + 7
嗯,就是這樣;我之前解決了這個問題,而另一個複製和粘貼作業將它帶回來。我現在使用LINQPad刪除並重新創建SP,因爲這比Server Explorer更容易。 –