用PL/SQL语言编写一程序,实现按部门分段(6000以上、(6000,3000)、3000元以下)统计各工资段的职工人数、以及各部门的工资总额(工资总额中不包括奖金)
-
输出到一张表中
-
直接输出在屏幕上
答:
(1)输出到一张表中
create table salcount
(deptno number, --部门号
sg1 int, --3000以下的人数
sg2 int, – 3000~6000的人数
sg3 int, – 6000以上的人数
sumsal number—工资总额
);
declare
–定义两个游标保存结果
cursor c1 is select distinct deptno from dept;
cursor c2(pdno number) is select sal
from emp where deptno=pdno;
–定义三个变量用于保存每个部门三个工资段的人数
count1 number;
count2 number;
count3 number;
saltotal number;
–记录c1游标中的部门号
pdeptno dept.deptno% TYPE;
–记录c2游标中的薪水值
psal emp.sal% TYPE;
begin
select sum(sal) into saltotal from emp;
open c1;–打开c1 获得所有部门号
loop
fetch c1 into pdeptno;–取一个部门号
exit when c1%notfound;
–计数器清零
count1 := 0;
count2 := 0;
count3 :=