Datediff 语句中的用例
Use Case in Datediff statement
你能帮我解决以下问题吗:
考虑以下 2 个汽车维修公司的表格:
Old_Value New_Value Created
0 1 2016/09/14
1 2 2016/09/15
2 3 2016/09/19
Value Description
0 Not Diagnosed Yet
1 In Queue
2 In Progress
3 Fixed
我正在尝试计算处理时间,这是 "In Progress" 和 "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 tb1
我得到的只是一条错误消息。
有什么帮助吗,谢谢。
不清楚你想要什么,但试试这个它会 return 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
我刚刚 运行 你的查询,但没有发现错误。它只是 return 将所有 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 中,您的参数之一始终为 null,returns null。
SELECT DateDiff(DAY, NULL, NULL) --return NULL
SELECT DateDiff(DAY, GETDATE(), NULL) --return NULL
SELECT DateDiff(DAY, NULL, GETDATE()) --return NULL
在 MySQL 中,您没有使用 day
。我希望这样的查询:
Select repair_id,
datediff(max(case when old_value = 1 and new_value = 2 then created end),
max(case when old_value = 2 and new_value = 3 then created end)
) as handle_time
from tb1
group by repair_id;
我发明了专栏 repair_id
,因为它对我来说很有意义。如果 table 中只有一项修复,则可以将其排除在查询之外。
你能帮我解决以下问题吗: 考虑以下 2 个汽车维修公司的表格:
Old_Value New_Value Created
0 1 2016/09/14
1 2 2016/09/15
2 3 2016/09/19
Value Description
0 Not Diagnosed Yet
1 In Queue
2 In Progress
3 Fixed
我正在尝试计算处理时间,这是 "In Progress" 和 "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 tb1
我得到的只是一条错误消息。 有什么帮助吗,谢谢。
不清楚你想要什么,但试试这个它会 return 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
我刚刚 运行 你的查询,但没有发现错误。它只是 return 将所有 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 中,您的参数之一始终为 null,returns null。
SELECT DateDiff(DAY, NULL, NULL) --return NULL
SELECT DateDiff(DAY, GETDATE(), NULL) --return NULL
SELECT DateDiff(DAY, NULL, GETDATE()) --return NULL
在 MySQL 中,您没有使用 day
。我希望这样的查询:
Select repair_id,
datediff(max(case when old_value = 1 and new_value = 2 then created end),
max(case when old_value = 2 and new_value = 3 then created end)
) as handle_time
from tb1
group by repair_id;
我发明了专栏 repair_id
,因为它对我来说很有意义。如果 table 中只有一项修复,则可以将其排除在查询之外。