想知道如何計算這個日期時間之間的間隔。我一直在 SQL Bigqury 中嘗試 Datetime_diff() 函式,但沒有成功。
| 開始時間 | 結束時間 |
|---|---|
| 2020-04-26 17:45:00 UTC | 2020-04-26 18:12:00 UTC |
| 2020-04-17 17:08:00 UTC | 2020-04-17 17:17:00 UTC |
| 2020-04-01 17:54:00 UTC | 2020-04-01 18:08:00 UTC |
我想添加一個新列,其中包含這次旅行的 DURATION。
uj5u.com熱心網友回復:
started_at并且ended_at看起來是 TIMESTAMP 型別,所以使用 TIMESTAMP_DIFF() 如下所示。
WITH sample_data AS (
SELECT TIMESTAMP '2020-04-26 17:45:00 UTC' started_at, TIMESTAMP '2020-04-26 18:12:00 UTC' ended_at UNION ALL
SELECT '2020-04-17 17:08:00 UTC', '2020-04-17 17:17:00 UTC' UNION ALL
SELECT '2020-04-01 17:54:00 UTC', '2020-04-01 18:08:00 UTC'
)
SELECT TIMESTAMP_DIFF(ended_at, started_at,MINUTE) duration FROM sample_data;

如果它是 STRING 型別,請將其轉換為 TIMESTAMP,如下所示。
TIMESTAMP_DIFF(TIMESTAMP(ended_at), TIMESTAMP(started_at), MINUTE)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/529790.html
標籤:Google Cloud Collective sql约会时间谷歌大查询间隔
