为什么不同的日期数学运算产生相同的值?
Why do different date math operations produce the same value?
我可以将日期加六天并得到我期望的结果,但如果我将同一日期加七天,我会得到相同的日期。
我的意思是这个;当用户输入“12/27/2015”作为@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
你确定吗:
DECLARE @BegDate datetime = GETDATE()
SELECT @BegDate, DATEADD(Day, 6, @BegDate), DATEADD(Day, 7, @BegDate);
结果:
我认为你的问题出在这里:
@Week1End Week1End,
@Week1End Week2Begin, ---<<<<<---
应该是:
@Week1End Week1End,
@Week2BeginWeek2Begin, ---<<<<<---
@Week1End Week1End,
@Week1End Week2Begin,
再看看这段代码...
我可以将日期加六天并得到我期望的结果,但如果我将同一日期加七天,我会得到相同的日期。
我的意思是这个;当用户输入“12/27/2015”作为@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
你确定吗:
DECLARE @BegDate datetime = GETDATE()
SELECT @BegDate, DATEADD(Day, 6, @BegDate), DATEADD(Day, 7, @BegDate);
结果:
我认为你的问题出在这里:
@Week1End Week1End,
@Week1End Week2Begin, ---<<<<<---
应该是:
@Week1End Week1End,
@Week2BeginWeek2Begin, ---<<<<<---
@Week1End Week1End,
@Week1End Week2Begin,
再看看这段代码...