Bigquery:有没有办法将时间戳向上或向下舍入到最近的分钟?

Liv*_*ire 4 mysql rounding google-bigquery

我一直在尝试在 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
Run Code Online (Sandbox Code Playgroud)

我想要的结果如下

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
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助。

Mik*_*ant 5

以下是 BigQuery 标准 SQL

#standardSQL
SELECT user_id, 
  TIMESTAMP_TRUNC(TIMESTAMP_ADD(created_at, INTERVAL 30 SECOND), MINUTE) created_at
FROM `project.dataset.table`  
Run Code Online (Sandbox Code Playgroud)

您可以使用问题中的示例数据进行测试,如下例所示

#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`
Run Code Online (Sandbox Code Playgroud)

有结果

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  
Run Code Online (Sandbox Code Playgroud)