表emp有如下字段:deptno, empid, name, job, sal。分别表示:部门编号、员工编号、员工姓名、职位、薪资。求每个部门的工资占比,结果用百分号表示,保留2位小数。以下代码正确的是( )
A. select deptno, sum(sal)/(select sum(sal) from emp) from emp;
B. select deptno, sum(sal)/(select sum(sal) from emp) from emp group by deptno;
C. select deptno, concat(round(sum(sal)/(select sum(sal) from emp)*100,2),“%”) from emp group by deptno;
D. select deptno, concat(round(sum(sal)/(select sum(sal) from emp),2),“%”) from emp group by deptno;
参考答案: C
解析:难度中上。分组后部门薪资总和占比为sum(sal)/(select sum(sal) from emp)以小数形式呈现;round(sum(sal)/(select sum(sal) from emp)*100,2)实现该值扩大100倍,保留2位小数;concat(round(sum(sal)/(select sum(sal) from emp)*100,2),“%”) 实现该值与“%”拼接。