Home > Software design >  I want to make a double column in SQL but it turn into a BIGINT instead
I want to make a double column in SQL but it turn into a BIGINT instead

Time:04-20

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)

CodePudding user response:

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

CodePudding user response:

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/total_cases)*100::double precision AS death_percentage 
FROM public.covid_deaths
ORDER BY location, date;
  • Related