以下是用于BigQuery标准SQL
#standardSQL
WITH windows AS (
SELECT start_day,DATE_ADD(start_day,INTERVAL 4 DAY) end_day
FROM UNNEST(GENERATE_DATE_ARRAY('2019-10-01',CURRENT_DATE(),INTERVAL 5 DAY)) start_day
)
SELECT start_day,end_day,trip.vehicle_id,ARRAY_AGG(trip ORDER BY trip.start_time) trips
FROM `project.dataset.table` trip
JOIN windows ON start_time BETWEEN start_day AND end_day
GROUP BY start_day,vehicle_id
您可以像下面的示例一样使用虚拟数据进行测试,玩游戏
#standardSQL
WITH `project.dataset.table` AS (
SELECT 'Vehicle1' vehicle_id,'Trip1' trip_id,DATE '2019-10-29' start_time,DATE '2019-10-30' stop_time UNION ALL
SELECT 'Vehicle1','Trip2','2019-10-30','2019-10-31' UNION ALL
SELECT 'Vehicle1','Trip3','2019-10-31','2019-11-01' UNION ALL
SELECT 'Vehicle1','Trip4','2019-11-01','2019-11-02' UNION ALL
SELECT 'Vehicle1','Trip5','2019-11-02','2019-11-03' UNION ALL
SELECT 'Vehicle1','Trip6','2019-11-03','2019-12-04' UNION ALL
SELECT 'Vehicle1','Trip7','2019-11-04','2019-12-05' UNION ALL
SELECT 'Vehicle2','Trip1','2019-10-29','2019-10-30' UNION ALL
SELECT 'Vehicle2','2019-10-31' UNION ALL
SELECT 'Vehicle2','2019-11-01' UNION ALL
SELECT 'Vehicle2','2019-11-02'
),windows AS (
SELECT start_day,vehicle_id
-- ORDER BY start_day,vehicle_id
有结果
本文链接:https://www.f2er.com/3163649.html