我一直在尝试在Bigquery中向上和向下舍入到NEAREST分钟。有谁知道实现此目标的最佳功能和方法?
user_id | created_at
-------------------------------------
14451 | 2019-01-31 04:51:28 UTC
14452 | 2019-01-31 04:51:31 UTC
14453 | 2019-01-31 04:51:59 UTC
14454 | 2019-01-31 04:51:03 UTC
我想要的结果如下
user_id | created_at
-------------------------------------
14451 | 2019-01-31 04:51:00 UTC
14452 | 2019-01-31 04:52:00 UTC
14453 | 2019-01-31 04:52:00 UTC
14454 | 2019-01-31 04:51:00 UTC
非常感谢您的帮助。
答案 0 :(得分:3)
以下是用于BigQuery标准SQL
#standardSQL
SELECT user_id,
TIMESTAMP_TRUNC(TIMESTAMP_ADD(created_at, INTERVAL 30 SECOND), MINUTE) created_at
FROM `project.dataset.table`
您可以使用问题中的示例数据来测试,玩这个游戏,如以下示例所示
#standardSQL
WITH `project.dataset.table` AS (
SELECT 14451 user_id, TIMESTAMP '2019-01-31 04:51:28 UTC' created_at UNION ALL
SELECT 14452, '2019-01-31 04:51:31 UTC' UNION ALL
SELECT 14453, '2019-01-31 04:51:59 UTC' UNION ALL
SELECT 14454, '2019-01-31 04:51:03 UTC'
)
SELECT user_id,
TIMESTAMP_TRUNC(TIMESTAMP_ADD(created_at, INTERVAL 30 SECOND), MINUTE) created_at
FROM `project.dataset.table`
有结果
Row user_id created_at
1 14451 2019-01-31 04:51:00 UTC
2 14452 2019-01-31 04:52:00 UTC
3 14453 2019-01-31 04:52:00 UTC
4 14454 2019-01-31 04:51:00 UTC
答案 1 :(得分:1)
在执行DIV之前,只需将60的一半(值30)相加
SELECT user_id ,SEC_TO_TIME(((TIME_TO_SEC(created_at)+30) DIV 60) * 60) FROM `table`