0

so I'm currently learning SQL and one of the exercies was to:

Show patient_id, weight, height, isObese from the patients table. Display isObese as a boolean 0 or 1. Obese is defined as weight(kg)/(height(m)2) >= 30. Weight is in units kg and height in units cm.

I managed to do it this way:

SELECT patient_id, weight, height,
CASE
WHEN weight/POWER(height/100,2) >= 30 then '1'
ELSE '0' END AS isObese
FROM patients

and it was incorrect. The correct solution was to write: weight/POWER(height/100.0,2). So my question is why I have to divide by 100.0 instead of 100 to change from centimeters to meters? What is the difference? In this table every height value was an int type, without decimal numbers.

asia
  • 13
  • 4
  • FYI, `100.0` is *not* a (literal) `float`, but a (literal) `decimal`. As for your qusetion, what are the data types for `weight` and `height`? I assume that they are also `int` values? – Thom A Jun 15 '22 at 11:26
  • @Larnu yes, weight and height are in int type – asia Jun 15 '22 at 11:35
  • This is because of truncation, height is an int data type. If you divide int by int, the decimals will be truncated, ex: 100/3 = 33 BUT 100/3.0 = 33.333333333 – Ming Sep 12 '22 at 04:48
  • or use ; weight*10000/(power(height,2)) – M. John Mar 20 '23 at 01:53

0 Answers0