oracle - SQL to find the average amount of money spent by country? -
i need find average amount of money spent country, using 2 tables below in oracle. sale_total money spent in each sale , cust_country customer's country. appreciated.
tables
sale
sale_id payment_id ship_id customer_id sale_total ==> money spent sale_date sale_time
customer
cust_name cust_address cust_city cust_country cust_phone cust_age cust_sex
you need have link between sale , customer tables. presumably place customer_id in customer table primary key, , have customer_id in sale table foreign key. assuming that, can run below query.
select avg(s.sale_total) spent, c.cust_country sale s inner join customer c on s.customer_id=c.customer_id group c.cust_country this tells average amount spent, , country spent.
the key understanding answer avg (the average function) aggregate function, combines things. when have aggregate function, have group other columns, hence why included group by clause.
the reason need customer id in customer table (apart being logical place it) can establish relationship between customer , sale tables.
another way establish relation can create new table link customer , sale together. call customer sales. example:
customer_sales
customer_id sale_id then you'd adjust query join based on that.
Comments
Post a Comment