sql - Percentage calculation when size is same but weight is different -


i want weight percentage of total weight each person.

here's data:

tablea  name        |   size    |   weight   ------------------------------------   jamie       |   0.25    |   48   jamie       |   0.50    |   48   taylor      |   0.25    |   55   taylor      |   0.30    |   54   taylor      |   0.45    |   48   taylor      |   0.45    |   60   

and here's output like:

name    |   size        |   percentage  |   weight   ---------------------------------------------------------------   jamie   |   0.25        |   50%     |   48   jamie   |   0.50        |   50%     |   48   taylor  |   0.25        |   35.03%      |   55   taylor  |   0.30        |   34.39%      |   54   taylor  |   0.45        |   49.76%      |   108   

eg :
total weight jamie 96 (48+48), have calculate weight values percentage of 96 each row.

and taylor there size 0.45 twice different weight in output should sum weight (48+60) , (108/217)*100=49.76 percentage

i suggest need summarize source records first, calculate percentage. so, using derived table propose sum weight name/size, , finish in outer query.

select       name     , size     , 100 * weight / sum(weight) on (partition name) per (       select             name           , size           , sum(weight) weight       worker       group             name           , size ) w 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -