如何在PostgreSQL中获取两个日期之间的平均值

编程入门 行业动态 更新时间:2024-10-25 14:32:00
本文介绍了如何在PostgreSQL中获取两个日期之间的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在Postgres中获取两个日期之间的平均日期:

I'd like to get the average date between two dates in Postgres:

在SqlServer中,通过以下简单查询即可完成操作:

In SqlServer is done with this simple query:

SELECT DateAdd(ms, DateDiff(ms, date_begin, date_end)/2, date_begin)

例如:

SELECT DateAdd(ms, DateDiff(ms, getdate(), dateadd(d, 1, getdate()))/2, getdate())

现在,我想在postgres中进行相同的查询。 我尝试使用类似的

Now I'd like to make the same query in postgres. I try with something like

SELECT current_timestamp + ((SELECT ((DATE_PART('day', (current_timestamp + INTERVAL '1 day')::timestamp - current_timestamp::timestamp) * 24 + DATE_PART('hour', (current_timestamp + INTERVAL '1 day')::timestamp - current_timestamp::timestamp)) * 60 + DATE_PART('minute', (current_timestamp + INTERVAL '1 day')::timestamp - current_timestamp::timestamp)) * 60 + DATE_PART('second', (current_timestamp + INTERVAL '1 day')::timestamp - current_timestamp::timestamp)||'second')::interval

不起作用。

在Postgres中,有没有更简单的方法来进行此简单操作?

Is there a simpler way to make this simple operation in Postgres?

推荐答案

怎么样:

select date_begin + (date_end - date_begin)/2

更多推荐

如何在PostgreSQL中获取两个日期之间的平均值

本文发布于:2023-10-25 18:29:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527743.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:平均值   两个   日期   如何在   PostgreSQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!