TSQL Query returns values for each hour for the past 24 hours

前端 未结 2 1301
隐瞒了意图╮
隐瞒了意图╮ 2021-01-22 20:48

I have a query that I don\'t really know how to begin with. I\'m hoping someone can help me out with it. I will start by explaining the table

I have a device table with

2条回答
  •  星月不相逢
    2021-01-22 21:02

    Something like this might get you started.

    select  SUM(datediff(second, Begin_dt, End_dt)) as seconds_online
            ,DATEPART(hour, Begin_dt) as [hour]
    from table
    where device_status in (1,4,5)
    group by DATEPART(hour, Begin_dt)
    

    To format the result you may want to follow this question:

    SQL - Seconds to Day, Hour, Minute, Second

提交回复
热议问题