mysql - Return the proportionate share of the same type -


just example lets have table sales(id,type,price).

now, each entry have return proportion of type sellings.

for instance, if total revenue selling bikes 1000, , sold 1 pair of bike 100 it's proportion 0.1.

how implement on sql? thanks!

set up:

create table sales (     id numeric,     type varchar(20),     price decimal );  insert sales values (1,'bike','900.00'); insert sales values (2,'bike','100.00'); 

query:

select s1.id, s1.type, s1.price, (s1.price/s2.sum_price) proportion sales s1 inner join (     select type, sum(price) sum_price     sales     group type ) s2 on s1.type = s2.type; 

the inner query gets sums type. emulation of sum(col2) on (partition col2) available in databases not in mysql.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -