Averaging dates in oracle sql

user1261700 picture user1261700 · Apr 25, 2012 · Viewed 21.9k times · Source

Is there a way to average multiple dates in oracle? avg doesn't do any good.

Thanks.

Answer

Dan A. picture Dan A. · Apr 25, 2012

The definition of an "average date" is subjective, but you could convert your dates to a Julian number, then average those, round it off, then convert back to a date.

create table dates (dt DATE);

insert into dates 
values ('24-APR-2012');
insert into dates 
values ('01-JAN-2012');
insert into dates 
values ('01-JAN-2013');
insert into dates
values ('25-DEC-1900');


select to_date(round(avg(to_number(to_char(dt, 'J')))),'J')
from dates;

Here's the SQL Fiddle: http://sqlfiddle.com/#!4/98ce9/1