Oracle doesn't allow `Avg` or `Sum` on interval data types. Seems like the
commonly accepted way to do so is with `EXTRACT(day from
24*60*60*interval)`. See http://stackoverflow.com/a/5063553/10583. That
only gets you to second resolution though, completely ignoring
microseconds. Not really ideal.
http://www.dba-oracle.com/t_timestamp_math_elapsed_times.htm is another
option which involves extracting each piece of the date component and
multiplying by a constant. That'll give microsecond precision (decimal
second component at least).
{{{
with the_interval as (
select INTERVAL '4 5:12:10.999999' DAY TO SECOND(6) itv from dual
)
select
itv,
-- convert back to interval after applying avg function
NUMTODSINTERVAL(avg(extract(day from itv)*86400 +
extract(hour from itv)*3600 +
extract(minute from itv)*60 +
extract(second from itv)), 'SECOND') seconds_with_precision,
extract(day from 24*60*60*itv) seconds_without_precision
from the_interval;
}}}
To support this in `AVG`, we'd need to create an `as_oracle()` method,
test if the `output_field` is an interval, and then wrap `output_field`
with multiple extracts. The `functions.Coalesce` does something similar
with `TextField`s, so it's not unprecedented.
--
Ticket URL: <https://code.djangoproject.com/ticket/24699>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by jarshwah):
I'm working on this at the moment. I think there are similar issues with
the SUM(interval) aggregate, so I'll do that if required too.
--
Ticket URL: <https://code.djangoproject.com/ticket/24699#comment:1>
* has_patch: 0 => 1
* version: 1.8 => master
Comment:
PR: https://github.com/django/django/pull/4699
--
Ticket URL: <https://code.djangoproject.com/ticket/24699#comment:2>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/24699#comment:3>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/24699#comment:4>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"c7805ee214802ff1c0de53660bd2594bc1abfebb" c7805ee]:
{{{
#!CommitTicketReference repository=""
revision="c7805ee214802ff1c0de53660bd2594bc1abfebb"
Fixed #24699 -- Added aggregate support for DurationField on Oracle
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24699#comment:5>