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.

steve
  • 5
  • 1
  • 3

1 Answers1

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 ...
sticky bit
  • 36,626
  • 12
  • 31
  • 42
  • you only need to cast one of the columns, e.g. Using `used_disk_size::decimal` would be enough –  Apr 23 '19 at 10:03
  • 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. – sticky bit Apr 23 '19 at 10:06
  • Thanks, that fixed the problem. – steve Apr 23 '19 at 10:17