Oracle
 sql >> Database >  >> RDS >> Oracle

Trova la media degli ID in un mese

Devi solo modificare il troncamento nella tua sottoquery per troncare per giorno anziché per mese, quindi troncare la query esterna per mese anziché per anno.

select id, to_char(event_day, 'Mon') event_month, sum(cnt) overall_count, avg(cnt) average_count
from (
    select id, trunc(event_date) event_day, count(*) cnt
    from daily 
    where event_date >= date '2019-01-01' and event_date < date '2019-01-31'
    group by id, trunc(event_date)
) t
group by id, to_char(event_month, 'Mon')