Home > other >  Is there a way to change a data type of a temporary column?
Is there a way to change a data type of a temporary column?

Time:10-06

I have a query where in I divide 2 columns and multiply it by 100 in a new column(DeathPercentage). But this new column data type is automatically set into bigint, is there a way that i could change it to decimal?

SELECT location, total_deaths, population.population, (total_deaths/population.population)*100 as DeathPercentage FROM covid_deaths
LEFT OUTER JOIN population
ON covid_deaths.location = population.country
WHERE covid_deaths.continent !='null'

enter image description here

CodePudding user response:

Postgres has cast, 2 versions. In this case you can:

(cast(total_deaths as numeric)/population.population)*100
                 OR
(total_deaths::numeric/population.population)*100

The first using the SQL standard and the second a Postgres extension.

CodePudding user response:

cast((total_deaths/population.population)*100) as numeric

CodePudding user response:

I don't know if your language have this, but Java have something call casting. That might work for your language!

  • Related