格式化为百分比

时间:2019-01-30 02:33:39

标签: sql sql-server

我一直在尝试一些不同的查询,但我似乎无法以百分比形式获得格式,我所划分的两个数字都是浮点数。

SELECT p.name, s.name, CAST(CAST(ROUND(r.result/s.record, 2)*100 as DECIMAL(18, 2)) as VARCHAR(100)) + ' %'  as percentage 
FROM people p, results r, sports s 
WHERE p.ID=r.peopleID AND s.ID = r.sportID;

SELECT p.name, s.name, FORMAT((r.result/s.record), 'Percent') as percentage 
FROM people p, results r, sports s 
WHERE p.ID=r.peopleID AND s.ID = r.sportID;

SELECT p.name, s.name, FORMAT((r.result/s.record), 'P3') as percentage 
FROM people p, results r, sports s 
WHERE p.ID=r.peopleID AND s.ID = r.sportID;

SELECT p.name, s.name, FORMAT(r.result/s.record)*100 as percentage 
FROM people p, results r, sports s 
WHERE p.ID=r.peopleID AND s.ID = r.sportID;

我希望此格式为90.32%,但是我似乎正在浮点数或整数1和0 0.5 = 0 和1.15 = 1

1 个答案:

答案 0 :(得分:3)

许多数据库都进行整数除法。您可以通过将值转换为非整数来解决此问题:

r.result * 1.0 / s.record

您还应该学习使用正确的,明确的,标准 JOIN语法。