控制Oracle查询结果中的十进制数字

时间:2018-02-12 14:47:46

标签: database oracle

我试图让我的结果(百分比)以一个或两个十进制数字显示 这是我的疑问:

SELECT (SELECT Count(*)
        FROM   table_name
        WHERE  condition_1
               AND condition_2) / (SELECT Count(*)
                                   FROM   table_name) * 100 AS Percentage
FROM   table_name
WHERE  rownum = 1  

结果是给了我: 5.8098402829885303891092292 / 8502519026691

有没有办法让我的结果看起来像5.8或5.80 而不会四舍五入到5.9或6

谢谢

3 个答案:

答案 0 :(得分:0)

您可以使用ROUND(percentage,1)

   SELECT ROUND (percentage, 1)
    FROM   (SELECT (SELECT COUNT(*)
                    FROM   table_name
                    WHERE  condition_1
                           AND condition_2) / (SELECT COUNT(*)
                                               FROM   table_name) * 100 AS
                   Percentage
            FROM   table_name
        WHERE  rownum = 1)  

答案 1 :(得分:0)

您可以尝试使用1作为参数的trunc函数,或者添加fm格式,最多添加两位小数:

SELECT ( to_char(trunc(
        (SELECT Count(*)
           FROM   table_name
          WHERE  condition_1
            AND condition_2) / (SELECT Count(*)
                               FROM   table_name) * 100 
          ),1),'fm999G999G990D00') Percentage
FROM   table_name
WHERE  rownum = 1;

答案 2 :(得分:0)

实际上使用ROUND(VALUE,4)

SELECT ROUND((SELECT Count(*)
        FROM   table_name
        WHERE  condition_1
               AND condition_2) / (SELECT Count(*)
                                   FROM   table_name),4) * 100 AS Percentage
FROM   table_name
WHERE  rownum = 1;

以我想要的方式显示结果,感谢Kaushik Nayak,谢谢大家