oracle - SQL average by customer per month -
the table 'transactions' list of transactions date
, customerid
, cost per unit (price
) , quantity
.
i need report groups transactions months , gives count, total income , average income per customer.
i'm having trouble figuring out how insert group customerid
select statement:
select extract(month date) month, count(*) purchases, sum(price*quantity) income, avg(sum(price*quantity)) <-- needs grouped customerid aswell month transactions date between i2 , i3 group extract(month date);
the closest can like:
select extract(month date) month, count(*) purchases, sum(price*quantity) income, ( select avg(sum(price*quantity)) transactions group customerid, extract(month date) ) transactions t date between i2 , i3 group extract(month date);
but approach stops making sense after think minute since nested select
return multiple rows. , if add where
clause , remove group date
in second select:
(select avg(sum(price*quantity)) transactions group customerid extract(month date) = month)
then return... i'm not sure what, wrong.
it's oracle database way (10g think).
you can using arithmetic . . . divide total number of customers:
select extract(month date) month, count(*) purchases, sum(price*quantity) income, sum(price*quantity) / count(distinct customerid) transactions date between i2 , i3 group extract(month date);