gpt4 book ai didi

mysql - 大查询 : Is there a way to round a timestamps UP or DOWN to the NEAREST minute?

转载 作者:行者123 更新时间:2023-11-29 04:32:58 25 4
gpt4 key购买 nike

我一直试图在 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

关于mysql - 大查询 : Is there a way to round a timestamps UP or DOWN to the NEAREST minute?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54453607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com