我可以从文档中看到DATETIME_ADD
仅适用于INT64
值,而不适用于TIME
个对象。
我有DATETIME
代表起点,然后是TIME
对象中的持续时间
WITH input AS (
SELECT
DATE(2018,03,05) AS start_date,
TIME(5,0,0) AS start_time,
TIME(8,0,0) AS duration
)
SELECT
*,
DATETIME(start_date,start_time) AS start_datetime,
DATETIME_ADD(
DATETIME_ADD(
DATETIME_ADD(
DATETIME(start_date,start_time),
INTERVAL EXTRACT(HOUR FROM duration) HOUR
),
INTERVAL EXTRACT(MINUTE FROM duration) MINUTE
),
INTERVAL EXTRACT(SECOND FROM duration) SECOND
) AS end_datetime
FROM input
有一种更好的方法可以将TIME
对象的3个值(小时,分钟,秒)添加到给定的DATETIME
对象中吗?
答案 0 :(得分:3)
以下是BigQuery Standard SQL
WITH input AS (
SELECT
DATE(2018,03,05) AS start_date,
TIME(5,0,0) AS start_time,
TIME(8,0,0) AS duration
)
SELECT
*,
DATETIME(start_date,start_time) AS start_datetime,
DATETIME_ADD(
DATETIME_ADD(
DATETIME_ADD(
DATETIME(start_date,start_time),
INTERVAL EXTRACT(HOUR FROM duration) HOUR
),
INTERVAL EXTRACT(MINUTE FROM duration) MINUTE
),
INTERVAL EXTRACT(SECOND FROM duration) SECOND
) AS end_datetime,
DATETIME_ADD(
DATETIME(start_date,start_time),
INTERVAL DATETIME_DIFF(DATETIME(start_date,duration), DATETIME(start_date), SECOND) SECOND
) end_datetime_nicer_way
FROM input