'I want to make a double column in SQL but it turn into a BIGINT instead

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)



Solution 1:[1]

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

Solution 2:[2]

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;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sergey
Solution 2