我有两个时间戳,我可以用TIMESTAMP_DIFF
得到两个时间戳的差,但我只能在天、小时、分钟等时间内得到差异。我想要的是两个在定制格式上的差异。例如,"3D:5H:23M:08S“或"03:05:23:08”或“3天,5小时,23分钟,8秒”。
这是否可能与BigQuery标准SQL有关?
发布于 2020-02-13 13:45:14
下面是BigQuery标准SQL
#standardSQL
WITH `project.dataset.table` AS (
SELECT TIMESTAMP '2020-02-13 13:28:50.532153 UTC' start, TIMESTAMP '2020-03-16 18:51:58.532153 UTC' finish UNION ALL
SELECT TIMESTAMP '2020-02-13 13:28:50.532153 UTC', TIMESTAMP '2020-02-16 18:51:58.532153 UTC'
)
SELECT -- start, finish,
FORMAT('%sD:%s', x, FORMAT_TIMESTAMP('%HH:%MM:%SS', y)) custom_format1,
FORMAT('%s:%s', x, FORMAT_TIMESTAMP('%H:%M:%S', y)) custom_format2,
FORMAT('%sdays ,%s', x, FORMAT_TIMESTAMP('%Hhours, %Mminutes, %Sseconds', y)) custom_format3
FROM `project.dataset.table`,
UNNEST([CAST(TIMESTAMP_DIFF(finish, start, DAY) AS STRING)]) x,
UNNEST([TIMESTAMP_SECONDS(TIMESTAMP_DIFF(finish, start, SECOND))]) y
带输出
Row custom_format1 custom_format2 custom_format3
1 32D:05H:23M:08S 32:05:23:08 32days ,05hours, 23minutes, 08seconds
2 3D:05H:23M:08S 3:05:23:08 3days ,05hours, 23minutes, 08seconds
发布于 2020-02-13 12:44:37
您可以将差异转换为时间戳,然后根据您的心境使用format_timestamp()
:
select TIMESTAMP_MILLIS(timestamp_diff(ts1, t23, millisecond))
这提供了一个时间戳值(可能)在1970年代,但您可能只感兴趣的天,小时,和较小的单位。
https://stackoverflow.com/questions/60208089
复制相似问题