I have a table of fatalities and populations that I need to normalize to fatalities per 100K people:
CREATE TABLE all_states (
state character varying(20) NOT NULL,
fatalities integer NOT NULL,
population integer NOT NULL
);
I want to calculate the fatality rate per 100,000 people, so I did:
SELECT *, (fatalities/population)*100000 AS rate
FROM all_states;
But what I get is just 0s:
STATE population fatalities rate
"Alabama" 4802740 75 0
"Alaska" 722718 39 0
"Arizona" 6482505 69 0
"Arkansas" 2937979 93 0
"California" 37691912 390 0
I feel like I'm missing something super simple here. I tried casting it as a float but that didn't change the results:
SELECT state, population, fatalities,
CAST(((fatalities/population)*100000) AS float) AS rate
FROM all_states;
Best Answer
The problem here is that / operator in integer division truncates the result, check Mathematical Functions and Operators
Try with:
Or