SQL Query to get count of cycles if it matches value of column in consecutive rows

前端 未结 3 926
死守一世寂寞
死守一世寂寞 2021-01-29 08:50

I have table named vehicledata which consists 3 columns: id, Veh No, and Veh Mode.

My data looks like this:



        
3条回答
  •  情话喂你
    2021-01-29 09:16

    @Pradeep - The logic to build this query will be to check only rows where vehicle is currently in drive mode and he was IDLE before that ROW. You don't have to worry about IDLE modes and also the continuous DRIVE modes. You only need to check the rows where a vehicle has transitioned from IDLE to DRIVE. I have used a case statement and a LAG windows function to mark only these records as 1 and everything else as 0. And then I just took a sum of this column for each vehicle.

    ------------------------
    WITH vehicles(id , Veh_No,  Veh_Mode) AS
    ( 
    SELECT 1 ,  'KA03-003',   'IDLE'  UNION
    SELECT 2 ,  'KA03-003',   'IDLE'  UNION
    SELECT 3 ,  'KA03-003',   'IDLE'  UNION
    SELECT 4 ,  'KA03-003',   'DRIVE' UNION
    SELECT 5 ,  'KA03-003',   'DRIVE' UNION
    SELECT 6 ,  'KA03-003',   'DRIVE' UNION
    SELECT 7 ,  'KA03-003',   'DRIVE' UNION
    SELECT 8 ,  'KA03-003',   'DRIVE' UNION
    SELECT 9 ,  'KA03-003',   'IDLE'  UNION
    SELECT 10,  'KA03-003',   'IDLE'  UNION
    SELECT 11,  'KA03-003',   'IDLE'  UNION
    SELECT 12,  'KA03-003',   'DRIVE' UNION
    SELECT 13,  'KA03-003',   'DRIVE' UNION
    SELECT 14,  'KA03-003',   'DRIVE' UNION
    SELECT 15,  'KA03-003',   'DRIVE' UNION
    SELECT 16,  'KA05-005',   'IDLE'  UNION
    SELECT 17,  'KA05-005',   'IDLE'  UNION
    SELECT 18,  'KA05-005',   'IDLE'  UNION
    SELECT 19,  'KA05-005',   'DRIVE' UNION
    SELECT 20,  'KA05-005',   'DRIVE' UNION
    SELECT 21,  'KA05-005',   'DRIVE' UNION
    SELECT 22,  'KA05-005',   'DRIVE' UNION
    SELECT 23,  'KA05-005',   'DRIVE' UNION
    SELECT 24,  'KA05-005',   'IDLE'  UNION
    SELECT 25,  'KA05-005',   'IDLE'  UNION
    SELECT 26,  'KA05-005',   'IDLE'  UNION
    SELECT 27,  'KA05-005',   'DRIVE' UNION
    SELECT 28,  'KA05-005',   'DRIVE' UNION
    SELECT 29,  'KA05-005',   'DRIVE' UNION
    SELECT 30,  'KA05-005',   'DRIVE'
    )
    
    SELECT veh_no, SUM(count_drive_cycle) AS drive_cycles
    FROM (SELECT *,
          CASE WHEN Veh_Mode = 'DRIVE'
                AND LAG(veh_mode,1,'DRIVE') OVER (PARTITION BY Veh_No ORDER BY id) = 'IDLE'   -- check if prev mode is IDLE
               THEN 1
               ELSE 0 END AS count_drive_cycle
          FROM vehicles
         ) A
    GROUP BY veh_no
    

提交回复
热议问题