How to retrieve trips from historical data?

后端 未结 2 1613
终归单人心
终归单人心 2021-01-14 02:33

I have the following table mytable in Hive:

id    radar_id     car_id     datetime
1     A21          123        2017-03-08 17:31:19.0
2     A21         


        
2条回答
  •  花落未央
    2021-01-14 02:59

    select  count(*) / count(distinct to_date(datetime))    as trips_per_day
    
    from   (select  radar_id
                   ,datetime
                   ,lead(radar_id) over w  as next_radar_id
                   ,lead(datetime) over w  as next_datetime                    
    
            from    mytable
    
            where   radar_id in ('A21','B15')
    
            window  w as 
                    (
                        partition by  car_id
                        order by      datetime
                    )
            ) t
    
    where   radar_id        = 'A21'
        and next_radar_id   = 'B15'
        and datetime + interval '30' minutes >= next_datetime
    ;
    

    +----------------+
    | trips_per_day  |
    +----------------+
    | 1.5            |
    +----------------+
    

    P.s.
    If your version does not support intervals, the last code record could be replaced by -
    and to_unix_timestamp(datetime) + 30*60 > to_unix_timestamp(next_datetime)

提交回复
热议问题