Declare i number; j number; mark number; cout number:=0; Begin for i in 1..1000 Loop mark :=0; for j in 2..i/2 Loop if (i mod j = 0 )Then mark:=1; exit; end if; end Loop; if (mark = 0) Then cout:=cout+1; dbms_output.put(i||‘,‘); if mod(cout,5)=0 Then dbms_output.new_line(); end if; end if; end Loop; dbms_output.new_line(); dbms_output.put_line(‘合计共有‘||cout||‘个‘); End; /
declare v_high_sal employees.salary%TYPE; v_high_job jobs.job_title%TYPE; v_high_count number; v_low_sal employees.salary%TYPE; v_low_job jobs.job_title%TYPE; v_low_count number; begin select * into v_high_job,v_high_sal,v_high_count from (select job_title,avg(salary),count(*) from employees,jobs where employees.job_id=jobs.job_id group by job_title order by avg(salary) desc ) where rownum=1; select * into v_low_job,v_low_sal,v_low_count from (select job_title,avg(salary),count(*) from employees,jobs where employees.job_id=jobs.job_id group by job_title order by avg(salary)) where rownum=1; dbms_output.put_line(‘平均工资最高的职位是‘||v_high_job||‘,‘||‘平均最高工资是‘||v_high_sal||‘,‘||‘共有‘||v_high_count||‘人担任此职‘||‘;‘); dbms_output.put_line(‘平均工资最低的职位是‘||v_low_job||‘,‘||‘平均最高工资是‘||v_low_sal||‘,‘||‘共有‘||v_low_count||‘人担任此职‘||‘;‘); dbms_output.put_line(‘平均工资处于两级的职位平均工资的差别为‘||(v_high_sal-v_low_sal)||‘;‘); end; /
declare cursor curTest is select job_title,min_salary,max_salary,count(*) from jobs, employees where jobs.job_id=employees.job_id group by job_title,min_salary,max_salary; type rec_type is record ( job_name jobs.job_title%type, min_sal jobs.min_salary%type, max_sal jobs.max_salary%type, cout number ); v_JobTitle rec_type; times number(3,1); e_NOT_SIMILAR EXCEPTION; begin open curTest; fetch curTest into v_JobTitle; Loop begin exit when curTest%NOTFOUND; times := (v_JobTitle.max_sal/v_JObTitle.min_sal); if(times<=1.5) then dbms_output.put_line(‘职位‘||v_JobTitle.job_name||‘的最低工资为‘||v_JObTitle.min_sal||‘,‘||‘最高工资为‘||v_JobTitle.max_sal||‘,‘||‘两者的差别为‘||times||‘倍‘||‘,‘||‘供职此职位的人有‘||v_JobTitle.cout||‘,‘||‘工资变化区间正常‘); else raise e_NOT_SIMILAR; end if; EXCEPTION when e_NOT_SIMILAR then dbms_output.put_line(‘职位‘||v_JobTitle.job_name||‘工资变化区间过大‘); end; fetch curTest into v_JobTitle; end Loop; close curTest; end; /
with dept_job as (select dname, case job when ‘CLERK‘ then sal end CLERK, case job when ‘SALESMAN‘ then sal end SALESMAN, case job when ‘PRESIDENT‘ then sal end PRESIDENT, case job when ‘MANAGER‘ then sal end MANAGER, case job when ‘ANALYST‘ then sal end ANALYST from emp join dept on emp.deptno=dept.deptno) select dname,NVL(to_char(avg(CLERK)),‘无‘) CLERK,NVL(to_char(avg(SALESMAN)),‘无‘) SALESMAN,NVL(to_char(avg(PRESIDENT)),‘无‘) PRESIDENT, NVL(to_char(avg(MANAGER)),‘无‘) MANGER,NVL(to_char(avg(ANALYST)),‘无‘) ANALYST from dept_job group by dname;
select dname,CLERK,SALESMAN,PRESIDENT,MANAGER,ANALYST from (select dname,sal,job from emp join dept on emp.deptno=dept.deptno) dept_job pivot (avg(sal) for job in(‘CLERK‘ as CLERK,‘SALESMAN‘ as SALESMAN, ‘PRESIDENT‘ as PRESIDENT,‘MANAGER‘ as MANAGER,‘ANALYST‘ as ANALYST) )p
时间: 2024-10-05 15:54:10