2017-06-16 62 views
0

我有這個表:SUM()

Big query table

的TripDrivingDuration列是一個字符串類型的專欄中,我試圖將字符串轉換爲時間,然後使之和,但沒有按」工作。

Failed attempt

誰能幫助我?

+0

你期待的結果是什麼?對時間戳進行求和是沒有意義的。 –

+0

嗨艾略特, 該列意味着旅行的持續時間,所以我想要得到的總持續時間deviceId按日期分組 –

回答

0

這是否適合您?

WITH DATA as(
select 29 as VehicleId, '2017-01-02T 15:57:063000' as TripDateTime, '00:00:28' as TripDrivingDuration union all 
select 29 as VehicleId, '2017-01-02T 13:23:12' as TripDateTime, '00:00:21' as TripDrivingDuration union all 
select 29 as VehicleId, '2017-01-02T 17:23:05.063000' as TripDateTime, '00:07:11.9370000' as TripDrivingDuration union all 
select 29 as VehicleId, '2017-01-02T 14:59:44.063000' as TripDateTime, '00:08:19.9370000' as TripDrivingDuration union all 
select 29 as VehicleId, '2017-01-02T 15:45:44.063000' as TripDateTime, '00:01:22.9370000' as TripDrivingDuration union all 
select 29 as VehicleId, '2017-01-02T 15:17:58.063000' as TripDateTime, '00:03:29.9370000' as TripDrivingDuration union all 
select 29 as VehicleId, '2017-01-02T 18:52:15' as TripDateTime, '00:01:26' as TripDrivingDuration union all 
select 29 as VehicleId, '2017-01-02T 17:05:24.063000' as TripDateTime, '00:13:09.9370000' as TripDrivingDuration 
) 

SELECT 
    VehicleId, 
    SUM(UNIX_SECONDS(PARSE_TIMESTAMP('%H:%M:%S', REGEXP_EXTRACT(TripDrivingDuration, r'(.*)\.\d+')))) elapsed_time 
FROM data 
GROUP BY VehicleId 
+0

非常感謝你這會幫助我很多。這是答案 –