Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Averaging dates in oracle sql

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

Thanks.

like image 500
user1261700 Avatar asked Apr 24 '12 23:04

user1261700


People also ask

What are the different types of date/time averaging in SQL?

There are several types of date/time averaging in SQL. You can either average the date/time stamp as a whole, the date part of the date/time stamp, or the time portion of the date/time stamp.

What are the most commonly used Oracle date functions?

This page provides you with the most commonly used Oracle date functions that help you handle date and time data easily and more effectively. Add a number of months (n) to a date and return the same day which is n of months away. Extract a value of a date time field e.g., YEAR, MONTH, DAY, … from a date time value.

Is there a way to average multiple dates in Oracle?

Is there a way to average multiple dates in oracle? avg doesn't do any good. Thanks. Show activity on this post. 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.

How to do date arithmetic in Oracle?

Oracle handles some date arithmetic naturally - eg TRUNC (SYSDATE) + 1 will return tomorrow's date. 3) compare the average back to SYSDATE & convert back to date.


1 Answers

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

like image 126
Dan A. Avatar answered Sep 28 '22 06:09

Dan A.