尚不清楚你想要什麼,但這種嘗試將返回4.
Select TOP 1
datediff(DAY, (SELECT CREATED FROM T1 WHERE New_Value = 2), (SELECT CREATED FROM T1 WHERE New_Value = 3)) as handle_time
from t1
和我剛纔運行查詢,但沒有發現任何錯誤。它只是將所有3行返回爲NULL。
CREATE TABLE T1(Old_Value int, New_Value int, Created DATE)
CREATE TABLE T2(Value int, Description varchar(50))
INSERT INTO T1 VALUES
(0, 1, '2016/09/14'),
(1, 2, '2016/09/15'),
(2, 3, '2016/09/19')
INSERT INTO T2 VALUES
(0, 'Not Diagnosed Yet'),
(1, 'In Queue'),
(2, 'In Progress'),
(3, 'Fixed')
Select
datediff(day, (case when Old_Value='1' and New_Value='2' then Created else Null end), (case when Old_Value='2' and New_Value='3' then Created else Null end))
as Handle_Time
from T1
DROP TABLE T1
DROP TABLE T2
結果:
Handle_Time
NULL
NULL
NULL
現在只需要運行下面的查詢
Select
(case when Old_Value='1' and New_Value='2' then Created else Null end) AS param1,
(case when Old_Value='2' and New_Value='3' then Created else Null end) AS param2,
datediff(day, (case when Old_Value='1' and New_Value='2' then Created else Null end), (case when Old_Value='2' and New_Value='3' then Created else Null end))
as Handle_Time
from T1
結果:
param1 param2 Handle_Time
NULL NULL NULL
2016-09-15 NULL NULL
NULL 2016-09-19 NULL
因此,在您的參數DATEDIFF一個總是空返回空。
SELECT DateDiff(DAY, NULL, NULL) --return NULL
SELECT DateDiff(DAY, GETDATE(), NULL) --return NULL
SELECT DateDiff(DAY, NULL, GETDATE()) --return NULL
預期結果是什麼。另外'DATEDIFF'是'SQL SERVER'函數 –
預期的結果是一個Integer格式。 即Handle_time是4天。這是狀態更改爲「進行中」和更改爲「固定」之間的日期差異。 結果應該如下: _italic_ 'Handle_Time 4' – Ray
第一個表格是否只有三行?如果你有更多的行,幾乎沒有辦法找出哪些值匹配。 –