如何使用Microsoft SQL计算持续时间?

时间:2017-09-01 15:08:25

标签: sql sql-server rdbms datediff

我想从一个开始时间找到每个人的持续时间。我想计算每天的1个开始时间和多个用户的多个结束时间的持续时间。这是我的代码:

SELECT *,
      CAST(DATEDIFF(n, CAST(End_Time AS datetime), 
      CAST(Start_Time AS datetime))  AS FLOAT) / 60 AS Time_Duration
FROM 
     ( SELECT  NAME,
               MAX(CASE WHEN DESCRIPTION = 'Green' THEN Final_Value END) AS Start_Time,
               MAX(CASE WHEN DESCRIPTION = 'Red' THEN Final_Value END) AS End_Time
       FROM mydata
       WHERE NAME != ‘NA’
       GROUP BY NAME
     ) C

我无法获得任何持续时间的结果。 这就是我的输出:

    Name        Start_time           End_time        Time_Duration 
1   Day_1       5/6/15 2:30
2   John                           5/6/15 3:30          
3   Ben                            5/6/15 4:30          
4   Mike                           5/6/15 5:30          
5   Day_2       5/7/15 2:30
6   John_2                         5/7/15 4:30          
7   Ben_2                          5/7/15 5:30          
8   Mike_2                         5/7/15 6:30        

我希望它看起来像这样:

    Name        Start_time           End_time        Time_Duration 
1   Day_1      5/6/15 2:30
2   John                           5/6/15 3:30          1.00
3   Ben                            5/6/15 4:30          2.00
4   Mike                           5/6/15 5:30          3.00
5   Day_2      5/7/15 2:30
6   John_2                         5/7/15 4:30          2.00
7   Ben_2                          5/7/15 5:30          3.00
8   Mike_2                         5/7/15 6:30          4.00

1 个答案:

答案 0 :(得分:1)

假设name列中的值具有日期编号的后缀(第1天没有)

WITH td AS
(
SELECT *,
       ROW_NUMBER() OVER (PARTITION BY [day] ORDER BY final_value) rnum
  FROM (SELECT *,
               CASE WHEN CHARINDEX('_', name) = 0
                    THEN '1'
                    ELSE SUBSTRING(name, CHARINDEX('_', name) + 1, LEN(name) - CHARINDEX('_', name))
                END [day]           
           FROM t_dur
        ) tt
)

SELECT t1.name, 
       CASE WHEN rnum = 1 THEN t1.final_value END start_time,
       CASE WHEN rnum <> 1 THEN t1.final_value END end_time,
       CASE CAST(DATEDIFF(hour, (SELECT t2.final_value FROM td t2 WHERE t2.[day] = t1.[day] AND t2.rnum = 1),
                       t1.final_value) AS DECIMAl(5,2)) 
            WHEN 0 THEN NULL
            ELSE CAST(DATEDIFF(hour, (SELECT t2.final_value FROM td t2 WHERE t2.[day] = t1.[day] AND t2.rnum = 1),
                       t1.final_value) AS DECIMAl(5,2)) 
         END time_duration
  FROM td t1

结果

name    start_time              end_time                time_duration
Day_1   2015-05-06 02:30:00.000 NULL                    NULL
John    NULL                    2015-05-06 03:30:00.000 1.00
Ben     NULL                    2015-05-06 04:30:00.000 2.00
Mike    NULL                    2015-05-06 05:30:00.000 3.00
Day_2   2015-05-07 02:30:00.000 NULL                    NULL
John_2  NULL                    2015-05-07 04:30:00.000 2.00
Ben_2   NULL                    2015-05-07 05:30:00.000 3.00
Mike_2  NULL                    2015-05-07 06:30:00.000 4.00