0

I have a table name meal

id name carbs protein fat

1  one   10    30      18   
2  one   17    4        2
3  one   27    6        7

Now I want to get Cumulative Average of carbs

my query is

SELECT (100*(sum(m.carbs)/(sum(m.carbs)+sum(m.fat)+sum(m.protein))))AS Percantage_carbs FROM `meal` AS m

My query gives result 52.2580645

But I t will be

52.8848076

Strawberry
  • 33,338
  • 13
  • 38
  • 57
user3706249
  • 1
  • 1
  • 2

2 Answers2

1

You Query will result in 44.628099173554 which is absolutely correct.

Total: 10+30+18+17+4+2+27+6+7 = 121
Carbs Only: 10+17+27 = 54

(54 / 121) * 100 = 44,63%

SQL Fiddle: http://sqlfiddle.com/#!2/3d6e5/3

Timur
  • 639
  • 6
  • 19
0

The result of the data given and the query you're using is: 44,628099174

I guess your query should be something like this (your missing the group by):

SELECT (
      100 *
      (sum(m.carbs)
      /
      (sum(m.carbs)+ sum(m.fat)+ sum(m.protein)))
    ) AS Percantage_carbs 
  FROM `meal` AS m
GROUP BY name
Packet Tracer
  • 3,824
  • 3
  • 23
  • 36