That would be
TRUNC (AVG (appt.contact_date - REF.entry_date) OVER (PARTITION BY ser2.prov_name), 1)
because:
- if CONTACT_DATE and ENTRY_DATE are of the DATE datatype (and yes, they should be), you don't have to (actually, must not) "convert" to date again.
- If they are strings (i.e. stored into VARCHAR2 columns), then yes - you should convert them to DATEs by applying the TO_DATE with the appropriate format mask
- difference between two DATE values is number of days, so no problem in AVG-ing that
- truncate is OK as well
EDIT
Some more info: here's an example which shows what's going on:
SQL> with
2 appt (contact_date) as
3 (select trunc(sysdate) from dual union
4 select null from dual union all
5 select null from dual),
6 ref (entry_date) as
7 (select trunc(sysdate - 5) from dual),
8 ser2 (prov_name) as
9 (select 'a' from dual)
10 select appt.contact_date, ref.entry_date, ser2.prov_name,
11 TRUNC (AVG (appt.contact_date - REF.entry_date) OVER (PARTITION BY ser2.prov_name), 1) avg_by_provider
12 from appt, ref, ser2;
CONTACT_ ENTRY_DA P AVG_BY_PROVIDER
-------- -------- - ---------------
30.04.18 25.04.18 a 5
25.04.18 a 5
25.04.18 a 5
SQL>
Although there are two NULL values, AVG still returns the result which is not NULL. How come? Because of ANSI SQL standard, which says that aggregate functions (such as AVG) ignore NULL values.