简体   繁体   中英

Round a SQL value output?

script:

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

return:

  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

I would like to round the decimals on the two values with decimals in the AVG(SAL)column. Thoughts/advice?

use round()

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

Try

    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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM