我有一个查询,它给出了列中空值的百分比:
SELECT
Count(*)
FROM
XYZ_Referrals
WHERE
referral_source IS NULL
) * 100.0 / (
Select
Count(*)
From
XYZ_Referrals
) as 'referral source fields that are populated'
但是,我只需要将值舍入和截断到小数点后两位。以下查询取整,但不截断:
SELECT
Round
(
(
(
SELECT
Count(*)
FROM
XYZ_Referrals
WHERE
referral_source IS NULL
)
* 100.0 / Count(*)
),2,2
)
as 'referral source fields that are populated'
FROM
XYZ_Referrals
返回值为5.600000000000
我尝试过强制转换,但没有成功。
SELECT
(
Cast
(
Round
(
(
(
SELECT
Count(*)
FROM
XYZ_Referrals
WHERE
referral_source IS NULL
)
* 100.0 / Count(*)
)
),2
AS DECIMAL(3,2)
)
)
as 'referral source fields that are populated'
FROM
XYZ_Referrals
任何帮助都是非常感谢的。
发布于 2018-07-09 16:06:34
您使用了正确的行,但DECIMAL
中的精度不正确:
SELECT CAST(5.600000000000 AS DECIMAL(10, 2))
这会将数字转换为具有2位小数位的10位精度。
https://stackoverflow.com/questions/51249721
复制