0

I have a table in a database which holds a value for space used, and a value for space available. What I want to do is find the % of used / free space.

select
  datetime AS "time",
  storage.name as metric,
  (used_disk_size / (available_disk_size + used_disk_size))*100 as "Total disk space"
from
  usage
inner join storage on usage.storage_id = storage.storage_id;

When I run the above I get 0s in all columns.

0

1 Answer 1

0

Likely the data types are integers. And a division of integers stays an integer. Cast one or all of the operands to decimal.

...
(used_disk_size::decimal / (available_disk_size::decimal + used_disk_size::decimal)) * 100 ...
Sign up to request clarification or add additional context in comments.

3 Comments

you only need to cast one of the columns, e.g. Using used_disk_size::decimal would be enough
Yes, that's right and why I wrote "one or all" (or actually an arbitrary number greater that or equal one would be even more correct). I just like to be verbose in such cases and therefore cast all of them.
Thanks, that fixed the problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.