舍入SQL值输出?

时间:2018-10-30 18:24:30

标签: sql oracle rounding

脚本:

SELECT DEPTNO, COUNT(EMPNO), SUM(SAL), AVG(SAL)
  FROM EMP
 GROUP BY DEPTNO
 ORDER BY AVG(SAL) DESC;

返回:

  DEPTNO    COUNT(EMPNO) SUM(SAL)   AVG(SAL)
 ---------  ------------ --------  -----------
    10            3       8750     2916.66667
    20            5      10875           2175
    30            6       9400     1566.66667
    40            1       1500           1500

  

我想用AVG(SAL)列中的小数点将两个值的小数点四舍五入。有想法/建议吗?

2 个答案:

答案 0 :(得分:3)

使用round()

SELECT DEPTNO, COUNT(EMPNO),round( SUM(SAL),2),round( AVG(SAL),2)
FROM EMP GROUP BY DEPTNO ORDER BY AVG(SAL) DESC;

答案 1 :(得分:0)

尝试

    SELECT DEPTNO, 
      COUNT(EMPNO), 
      SUM(CONVERT(DECIMAL(5,2),SAL)),  
      AVG(CONVERT(DECIMAL(5,2),SAL)) 
   FROM EMP GROUP BY DEPTNO 
   ORDER BY 
       AVG(CONVERT(DECIMAL(5,2),SAL)) DESC;