sqlisqlquery

I want to calculate machine mode time range from this table. How can I write a SQL query for this situation?


enter image description here

I could not add image


Solution

  • You can try do something like this:

    Select machinestate, 
    Lead(machinestate,1) OVER (PARTITION BY machinename ORDER BY time) as 
             next_machinestate, 
    Lead(time,1) OVER (PARTITION BY machinename ORDER BY time) - time as 
             time_range 
    from your_table