Reputation: 17
SELECT location, date, total_cases, total_deaths,
(total_deaths/total_cases)*100 AS death_percentage
FROM public.covid_deaths
ORDER BY location, date;
Here the death percentage multiplies a decimal like 0.0027384 for example but when I try the query it appears as 0 and the column type is BIGINT instead of a double. Does anyone know why this is?
(I'm using postgreSQL)
Upvotes: 0
Views: 102
Reputation: 1105
You have to cast at least one part of the expression to double
to get double result. Using 1.0 results only in single precision float.
Try:
SELECT location, date, total_cases, total_deaths,
total_deaths*100::double precision /total_cases AS death_percentage
FROM public.covid_deaths
ORDER BY location, date;
Upvotes: 0
Reputation: 5250
SELECT location, date, total_cases, total_deaths,
(total_deaths*1.0/total_cases)*100 AS death_percentage
FROM public.covid_deaths
ORDER BY location, date;
Could you please try this
Upvotes: 1