【问题标题】:How to get average between two dates in PostgreSql如何在 PostgreSql 中获取两个日期之间的平均值
【发布时间】:2017-07-17 10:10:23
【问题描述】:

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

在 SqlServer 中完成这个简单的查询:

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 中进行相同的查询。 我尝试使用类似的东西

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 中进行这个简单的操作?

【问题讨论】:

    标签: postgresql average date-difference


    【解决方案1】:

    怎么样:

    select date_begin + (date_end - date_begin)/2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-19
      相关资源
      最近更新 更多