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')
CodePudding user response:
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.