【问题标题】:Use Case in Datediff statementDatediff 语句中的用例
【发布时间】:2016-10-10 11:37:19
【问题描述】:

请您帮我解决以下问题: 考虑汽车维修公司的这两张表:

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

我正在尝试计算处理时间,即“进行中”和“已修复”状态之间的日期差:

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

我得到的只是一个错误消息。 请大家帮忙,谢谢。

【问题讨论】:

  • 预期结果是什么。 DATEDIFF 也是 SQL SERVER 函数
  • 预期结果是整数格式。即 Handle_time 是 4 天。这是状态更改为“进行中”与更改为“已修复”之间的日期差。结果应该是这样的:italic Handle_Time 4
  • 第一个表只有三行吗?如果您有更多行,则几乎无法确定哪些值匹配。
  • 是的,它只有 3 行。
  • 谢谢你们的帮助,很快。 @GordonLinoff

标签: mysql sql case datediff


【解决方案1】:

在 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 专栏,因为它对我来说很有意义。如果表中只有一个修复,您可以将其排除在查询之外。

【讨论】:

    【解决方案2】:

    不清楚你想要什么,但试试这个它会返回 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 中,您的参数之一始终为 null,返回 null。

    SELECT DateDiff(DAY, NULL, NULL) --return NULL
    SELECT DateDiff(DAY, GETDATE(), NULL) --return NULL
    SELECT DateDiff(DAY, NULL, GETDATE()) --return NULL
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多