I am trying to get the average number of months difference between two dates:
select client_id,
avg(12*(year(MAX(received_date))-year(min(received_date)))
+ MONTH(MAX(received_date))-MONTH(min(received_date)))
from tmpTwoAccessions
group by CLIENT_ID,PATIENT_ID
I am getting this message:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
Can you please advise me on what am I doing incorrectly here?
Thanks so much for your guidance.
You do the difference between the MAX and MIN in a sub-query, and then average that in the outer query.
I’m not convinced your date difference expression is optimal, but I’ve not tried changing it.
Yurly Rozhovetskiy suggested the expression:
which looks good to me. The revised query becomes: