0

i am trying to calculate the fat percentage from my data but I cannot get the query to work. The equation should be. " Fat * 9, then that result divided by calories and then that result * by 100. At the moment I have this but I cannot my result is always 0:

SELECT name, mfr, protein, calories, fat, (fat * 9 / calories) * 100   AS 'Fat Percentage'
 FROM cereals WHERE mfr IN ('K', 'G')
Andrew
  • 5
  • 3

1 Answers1

0

Just put an extra bracket, it will work as per your requirement.

Use the following code:

SELECT name, mfr, protein, calories, fat, ((fat * 9) / calories) * 100   AS 'Fat Percentage'
FROM cereals WHERE mfr IN ('K', 'G');

Even though, if it doesn't work. Please, mention the datatype of the 'calories', 'fat' used in the table.

Griffin
  • 65
  • 5
  • The datatypes are Integers. By adding new brackets and changing 9 to 9.0 it ia now giving me good results. Thanks ((fat * 9.0) / calories) * 100 – Andrew Nov 04 '22 at 10:51