gpt4 book ai didi

mysql - 从 MySQL 到 Postgres : Last Quarter Hour

转载 作者:行者123 更新时间:2023-11-29 10:59:14 27 4
gpt4 key购买 nike

我正在从 MySQL 迁移到 Postgres。我需要获取一个 DateTime 值(显然是 Postgres 中的时间戳)并确定最后一刻钟的值。我在这两种环境下都有一个解决方案,但我想知道是否有更紧凑或更优雅的 Postgres 解决方案。这是我的 MySQL 方法:

select cast(now() as date) + interval hour(now()) hour + interval (floor(minute(now()) / 15) * 15) minute as last_quarter_hour;

这是我的 Postgres 方法:

select date(current_timestamp) + date_part('hour',cast(current_timestamp as timestamp)) * interval '1 hour' + floor(date_part('minute',cast(current_timestamp as timestamp)) / 15) * 15 * interval '1 minute' as last_quarter_hour;

欢迎所有建议。

最佳答案

简单一点:

select
date_trunc('hour', current_timestamp) +
date_part('minute', current_timestamp)::int / 15 * interval '15 minute' as last_quarter_hour

关于mysql - 从 MySQL 到 Postgres : Last Quarter Hour,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42535931/

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