0

I have simple query

select round((bca_total_lean/(bca_total_meta + bca_total_fat))*100,0) as lean_mass_percent from x where...

as result, I see 0. for bca_total_lean/bca_total_fat result is ok. By bca_total_meta is 0.

All fields are integers and all results are greater than 0. The same operation in this data in PHP returns 83. I also tried without a round function. The same result. Separated data return by BD

  • meta : 77391
  • fat: 11892
  • lean: 74362

Any hints where is an error?

3 Answers 3

3

Integer division truncates fractional digits. Your expression returns a ratio between 0 and 1, which is always truncated to 0.

To get "percentage", first multiply by 100.

select round((bca_total_lean*100)/(bca_total_meta + bca_total_fat),0) as lean_mass_percent from x where...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the explanation.
2

Postgres does integer division. So, I would express this as:

select round(bca_total_lean * 100.0 / (bca_total_meta + bca_total_fat), 0) as lean_mass_percent
from x
where . . .

2 Comments

It works, thank you. Let me understand why my example doesn't work.
@jaroApp . . . Is there some part of "integer division" that you don't understand?
0

Try:

select round((bca_total_lean::numeric/(bca_total_meta::numeric + bca_total_fat::numeric))*100,0) as lean_mass_percent
from x where...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.