How to get average between two dates in PostgreSql

2019-08-25 03:04发布

问题:

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

In SqlServer is done with this simple query:

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

For example:

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

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

but it does not work.

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

回答1:

How about:

select date_begin + (date_end - date_begin)/2