Oracle SQL - Average Time Between Dates By Subject -
i'm working in oracle sql. have table ids , dates, , i'm trying find average time between dates subject. this.
table
subjectid date 1 8/01/2013 12:00:00 1 8/31/2013 12:00:00 1 9/10/2013 12:00:00 2 1/01/2010 12:00:00 2 1/21/2010 12:00:00
i need write query goes through table subjectid, records time between dates, , outputs average of averages, speak. in example, time between first , second rows (30 days) + time between second , third rows (10 days) / 2 (to average subject 1, = 20), , time between rows 4 , 5 (20 days) / 1 (to average subject 2), , output should average between (20 + 10) / 2 = 15.
the average difference between min , max values, divided 1 less count.
for data:
select subjectid, (case when count(*) > 1 (max(date) - min(date))/(count(*) - 1) end) avgdifference t group subjectid;
to overall average, use subquery:
select avg(avgdifference) (select subjectid, (case when count(*) > 1 (max(date) - min(date))/(count(*) - 1) end) avgdifference t group subjectid ) t
Comments
Post a Comment