sql - MySQL Query - SELECT (average of a category) AS "CATEGORY AVERAGE" -


objective: when user browses particular seller, display average along average of sellers similar category easy comparison.

example data:

seller | category |  qty |  sales   --------------------------------------------   harry  | mango    | 100  |  50000   john   | apple    | 75   |  50500   max    | mango    | 44   |  20000   ash    | mango    | 60   |  35000   lingo  | apple    | 88   |  60000   

required output: (when user browses ash)
quantity sold ash: 60
average quantity sold other mango sellers: 68 (avg of 100, 44 & 60)

average price of ash: 583.33 (35000 / 60)
average price of other mango sellers: 514.70 (weighted average of prices)

skeleton code:

select 'qty' 'qty',
(some code) 'avg qty',
('sales' / 'qty') 'price',
(some code) 'avg price'
'sales table'
'seller' = 'ash'

use:

select yt.qty,        x.cat_avg,        yt.sales/yt.qty avg_price,        null weighted_average    your_table yt   join (select t.category,                avg(t.qty) cat_avg           your_table t       group t.category) x on x.category = yt.category  yt.seller = 'ash' 

i'll fill in weighted average when supply equation, if possible.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -