Bigquery:有没有办法将时间戳向上或向下舍入到最近的分钟?
Bigquery: Is there a way to round a timestamps UP or DOWN to the NEAREST minute?
我一直试图在 Bigquery 中向上和向下取整到最近的分钟。有谁知道实现这个的最佳功能和方法吗?
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
非常感谢您的帮助。
以下适用于 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
在执行 DIV
之前只需添加 60 的一半(值为 30)
SELECT user_id ,SEC_TO_TIME(((TIME_TO_SEC(created_at)+30) DIV 60) * 60) FROM `table`
我一直试图在 Bigquery 中向上和向下取整到最近的分钟。有谁知道实现这个的最佳功能和方法吗?
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
非常感谢您的帮助。
以下适用于 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
在执行 DIV
之前只需添加 60 的一半(值为 30)SELECT user_id ,SEC_TO_TIME(((TIME_TO_SEC(created_at)+30) DIV 60) * 60) FROM `table`