forked from srikanthpragada/plsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob_summary_2.sql
More file actions
61 lines (51 loc) · 1.72 KB
/
job_summary_2.sql
File metadata and controls
61 lines (51 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-- function to return names of employees with highest salary in the given job
create or replace function get_top_employees(p_jobid varchar)
return varchar is
cursor empnames is
select first_name
from employees
where job_id = p_jobid and salary =
(select max(salary) from employees where job_id = p_jobid);
v_names varchar(100);
begin
v_names := '';
for namerec in empnames
loop
v_names := v_names || namerec.first_name || ',';
end loop;
return rtrim(v_names,',');
end;
---------------------------------------
-- Program to use the above function and display details of jobs
set serveroutput on
declare
cursor jobcur is
select job_id, job_title
from jobs
where job_id in
(select job_id from employees);
v_count number(3);
v_count_hist number(3);
v_avg_exp number(2);
v_avg_sal employees.salary%type;
v_names varchar(100);
begin
for jobrec in jobcur
loop
select avg(salary), count(*),
avg(floor(months_between(sysdate,hire_date)/12))
into v_avg_sal, v_count, v_avg_exp
from employees
where job_id = jobrec.job_id;
select count(*) into v_count_hist
from job_history
where job_id = jobrec.job_id;
v_names := get_top_employees(jobrec.job_id);
dbms_output.put_line('Job Title : ' || jobrec.job_title);
dbms_output.put_line('No. Employees : ' || v_count);
dbms_output.put_line('Avg Salary : ' || v_avg_sal);
dbms_output.put_line('Avg Exp : ' || v_avg_exp);
dbms_output.put_line('History Count : ' || v_count_hist);
dbms_output.put_line('Top Employee(s) : ' || v_names);
end loop;
end;