I am trying to run the following query which results me postgres error: division by zero
select
request_count,
response_count,
(response_count*100) / (request_count+response_count) AS proportion
from total_dummy_table;
How can I avoid and resolve the division by zero
error?
Tried to fix using the below query but getting the same result as above
SELECT
request_count,
response_count,
(response_count*100) / (request_count) AS proportion
FROM
total_dummy_table
ORDER BY
(response_count*100) /(CASE request_count WHEN 0 Then NULL ELSE request_count END) DESC NULLS FIRST
Please let me know where am I doing wrong, or how can I fix this. Thanks!
Result expectation:
The query should return me something like below:
Request_count | Response_count | Proportion
1603423 | 585706 | 36.52
Quick note: Table total_dummy_table does not have column name proportion
that is the addition to the result which have calculated proportion in it.
use NULLIF
something/NULLIF(column_name,0)
NULLIF(col,val)
is shorthand for
CASE WHEN col=val THEN NULL ELSE col