如何在SQL中的计算列中显示小数点后两位数?
how to display two digits after decimal point in a calculated column in SQL?
select *,
(TotalDeaths/TotalCases)*100 as PercentDeath,
(TotalDeaths/population)*100 as DeathPercentPopulation
from ( select location,
population,
sum(newcases) as TotalCases,
sum(newdeaths) as TotalDeaths
from deaths
where continent is not null
group by location
) as newtable
order by DeathPercentPopulation desc
“PercentDeath”和“DeathPercentPopulation”列的小数点后有几位数字。但是我只想显示小数点后两位数。如何在不再次嵌套此查询的情况下执行此操作?
您可以使用 ROUND() 函数:https://docs.microsoft.com/fr-fr/sql/t-sql/functions/round-transact-sql?view=sql-server-ver15
用例:
select *,
(ROUND(TotalDeaths/TotalCases)*100, 2) as PercentDeath),
(ROUND(TotalDeaths/population)*100, 2) as DeathPercentPopulation)
FROM ...
你可以使用 TRUNCATE
TRUNCATE(X,D)
Returns the number X, truncated to D decimal places. If D is 0, the
result has no decimal point or fractional part. D can be negative to
cause D digits left of the decimal point of the value X to become
zero.
像这样:
select *,
TRUNCATE((TotalDeaths/TotalCases)*100,2) as PercentDeath,
TRUNCATE((TotalDeaths/population)*100,2) as DeathPercentPopulation
from ( select location,
population,
sum(newcases) as TotalCases,
sum(newdeaths) as TotalDeaths
from deaths
where continent is not null
group by location
) as newtable
order by DeathPercentPopulation desc
或者您可以使用 ROUND
函数。
The ROUND() function in MySQL is used to round a number to a specified
number of decimal places. If no specified number of decimal places is
provided for round off, it rounds off the number to the nearest
integer.
Syntax :
ROUND(X, D) Parameter : This method accepts two parameters in the
syntax, as mentioned above and described below –
X : The number which to be rounded. D : Number of decimal places up to
which the given number is to be rounded. It is optional. If not given
it round off the number to the closest integer. If it is negative,
then the number is rounded to the left side of the decimal point.
select *,
ROUND((TotalDeaths/TotalCases)*100,2) as PercentDeath,
ROUND((TotalDeaths/population)*100,2) as DeathPercentPopulation
from ( select location,
population,
sum(newcases) as TotalCases,
sum(newdeaths) as TotalDeaths
from deaths
where continent is not null
group by location
) as newtable
order by DeathPercentPopulation desc
使用 FORMAT()
函数代替 ROUND
或 TRUNCATE
。它是为此而设计的。它还根据区域设置放入 'thousands separators'。
select *,
(TotalDeaths/TotalCases)*100 as PercentDeath,
(TotalDeaths/population)*100 as DeathPercentPopulation
from ( select location,
population,
sum(newcases) as TotalCases,
sum(newdeaths) as TotalDeaths
from deaths
where continent is not null
group by location
) as newtable
order by DeathPercentPopulation desc
“PercentDeath”和“DeathPercentPopulation”列的小数点后有几位数字。但是我只想显示小数点后两位数。如何在不再次嵌套此查询的情况下执行此操作?
您可以使用 ROUND() 函数:https://docs.microsoft.com/fr-fr/sql/t-sql/functions/round-transact-sql?view=sql-server-ver15
用例:
select *,
(ROUND(TotalDeaths/TotalCases)*100, 2) as PercentDeath),
(ROUND(TotalDeaths/population)*100, 2) as DeathPercentPopulation)
FROM ...
你可以使用 TRUNCATE
TRUNCATE(X,D)
Returns the number X, truncated to D decimal places. If D is 0, the result has no decimal point or fractional part. D can be negative to cause D digits left of the decimal point of the value X to become zero.
像这样:
select *,
TRUNCATE((TotalDeaths/TotalCases)*100,2) as PercentDeath,
TRUNCATE((TotalDeaths/population)*100,2) as DeathPercentPopulation
from ( select location,
population,
sum(newcases) as TotalCases,
sum(newdeaths) as TotalDeaths
from deaths
where continent is not null
group by location
) as newtable
order by DeathPercentPopulation desc
或者您可以使用 ROUND
函数。
The ROUND() function in MySQL is used to round a number to a specified number of decimal places. If no specified number of decimal places is provided for round off, it rounds off the number to the nearest integer.
Syntax :
ROUND(X, D) Parameter : This method accepts two parameters in the syntax, as mentioned above and described below –
X : The number which to be rounded. D : Number of decimal places up to which the given number is to be rounded. It is optional. If not given it round off the number to the closest integer. If it is negative, then the number is rounded to the left side of the decimal point.
select *,
ROUND((TotalDeaths/TotalCases)*100,2) as PercentDeath,
ROUND((TotalDeaths/population)*100,2) as DeathPercentPopulation
from ( select location,
population,
sum(newcases) as TotalCases,
sum(newdeaths) as TotalDeaths
from deaths
where continent is not null
group by location
) as newtable
order by DeathPercentPopulation desc
使用 FORMAT()
函数代替 ROUND
或 TRUNCATE
。它是为此而设计的。它还根据区域设置放入 'thousands separators'。