0% found this document useful (0 votes)
6 views

sql query - Copy - Copy (3)

The document contains PL/SQL code that calculates and displays the total number of active and terminated employees along with their respective salaries, grouped by department. It utilizes cursors to fetch employee data and outputs the results using DBMS_OUTPUT. Additionally, there are sections for counting employees under terminated managers and calculating bonuses based on work experience.

Uploaded by

er.vks
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

sql query - Copy - Copy (3)

The document contains PL/SQL code that calculates and displays the total number of active and terminated employees along with their respective salaries, grouped by department. It utilizes cursors to fetch employee data and outputs the results using DBMS_OUTPUT. Additionally, there are sections for counting employees under terminated managers and calculating bonuses based on work experience.

Uploaded by

er.vks
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

v_total_salary_active NUMBER := 0;

v_total_employees_terminated NUMBER := 0;
v_total_salary_terminated NUMBER := 0;

-- Cursor for active employees


CURSOR cur_active IS

ORDER BY NVL(dept_id, 0);

BEGIN
-- Display Active employee details
DBMS_OUTPUT.PUT_LINE('Details of Active employees');
DBMS_OUTPUT.PUT_LINE('-----------------------------------');
DBMS_OUTPUT.PUT_LINE('Department_id Total_employees Total_salary');

-- Process the active employees


FOR rec_active IN cur_active LOOP
DBMS_OUTPUT.PUT_LINE(rec_active.dept_id || ' ' || rec_active.total_employees
|| ' ' || rec_active.total_salary);

-- Accumulate totals for active employees


v_total_employees_active := v_total_employees_active +
rec_active.total_employees;
v_total_salary_active := v_total_salary_active + rec_active.total_salary;
END LOOP;

-- Display total employees and total salary for active employees


DBMS_OUTPUT.PUT_LINE('-----------------------------------');
DBMS_OUTPUT.PUT_LINE(v_total_employees_active || ' Total_employees');
DBMS_OUTPUT.PUT_LINE(v_total_salary_active || ' Total_salary');
DBMS_OUTPUT.PUT_LINE('-----------------------------------');

-- Display Terminated employee details


DBMS_OUTPUT.PUT_LINE('Details of Terminated employees');
DBMS_OUTPUT.PUT_LINE('-----------------------------------');
DBMS_OUTPUT.PUT_LINE('Department_id Total_employees Total_salary');

-- Process the terminated employees


FOR rec_terminated IN cur_terminated LOOP
DBMS_OUTPUT.PUT_LINE(rec_terminated.dept_id || ' ' ||
rec_terminated.total_employees || ' ' || rec_terminated.total_salary);

-- Accumulate totals for terminated employees


v_total_employees_terminated := v_total_employees_terminated +
rec_terminated.total_employees;
v_total_salary_terminated := v_total_salary_terminated +
rec_terminated.total_salary;
END LOOP;

-- Display total employees and total salary for terminated employees


DBMS_OUTPUT.PUT_LINE('-----------------------------------');
DBMS_OUTPUT.PUT_LINE(v_total_employees_terminated || ' Total_employees');
DBMS_OUTPUT.PUT_LINE(v_total_salary_terminated || ' Total_salary');
DBMS_OUTPUT.PUT_LINE('-----------------------------------');

END;
/
----------------------------------------------------------------
1- employee under terminated manager
---------------------------------------------
SET NULL "NULL"; SET FEEDBACK OFF; SET ECHO OFF; SET HEADING OFF; SET WRAP OFF; SET
LINESIZE 10000; SET TAB OFF; SET PAGES 0; SET DEFINE OFF; set serveroutput on;
declare act number; ter number; mgrname varchar2(200); mgr_sup varchar2(200);
superv varchar2(200); cursor curr is select emp_id, concat(emp_fname,concat('
',emp_lname)) as empname, emp_status from emp; cursor curr2 is select superv,
concat(e.emp_fname,concat(' ',e.emp_lname)) as mgrname, e.emp_status as stat,
e.mgr_id as mgr1 from emp e, emp a where a.mgr_id=e.emp_id; c1 curr%rowtype; c2
curr2%rowtype; begin select count(emp_id) into act from emp where
emp_status='Active'; select count(emp_id) into ter from emp where
emp_status='Terminated'; dbms_output.put_line('**Status Count**');
dbms_output.put_line('Active '||' '||act); dbms_output.put_line('Terminated '||'
'||ter); dbms_output.put_line('**Employees under Terminated Manager**');
dbms_output.put_line('emp_id emp_name Mgr_name Mgr_Status Mgr_Supervisor'); open
curr; open curr2; loop fetch curr into c1; fetch curr2 into c2; exit when curr
%notfound; if c2.stat='Terminated' and c1.emp_status='Active' then select
concat(emp_fname,concat(' ',emp_lname)) into superv from emp where
emp_id=c2.mgr1; dbms_output.put_line(c1.emp_id||' '||c1.empname||' '||c2.mgrname||'
'|| c2.stat||' '||superv); end if; end loop; end; / /* Enter your query below.
Please append a semicolon ";" at the end of the query */ exit;
----------------------------------
2- calculating bonus
-------------------------------
SET NULL "NULL"; SET FEEDBACK OFF; SET ECHO OFF; SET HEADING OFF; SET WRAP OFF;
SET LINESIZE 10000; SET TAB OFF; SET PAGES 0; SET DEFINE OFF; set serveroutput on;
declare incentive number; work_exp number; cursor curr is select emp_id,
concat(emp_fname,concat(' ',emp_lname)) as empname, emp_hiredate from emp where
emp_status='Active' and extract(month from to_date(emp_hiredate, 'yyyy-mm-dd'))=12;
c curr%rowtype; begin open curr; dbms_output.put_line('Employees with yearly
incentive amounts:'); dbms_output.put_line('**********');
dbms_output.put_line('Employee ID Name of the Employee Hire Date Incentive
Amount'); dbms_output.put_line('**********'); loop fetch curr into c; exit when
curr%notfound; work_exp:=MONTHS_BETWEEN(to_date('31/12/2020',
'dd/mm/yyyy'),c.emp_hiredate)/12; case when work_exp>13 then incentive:=8000; when
work_exp>11 then incentive:=5000; when work_exp>9 then incentive:=3000; when
work_exp>7 then incentive:=2000; when work_exp>4 then incentive:=1000; when
work_exp>0 then incentive:=400; else incentive:='null'; end case;
dbms_output.put_line(c.emp_id||' '||c.empname||' '||c.emp_hiredate||' '||
incentive); end loop; dbms_output.put_line('**********'); dbms_output.put_line('The
number of rows fetched is '||curr%rowcount); dbms_output.put_line('**********');
end; / /* Enter your query below. Please append a semicolon ";" at the end of the
query */ exit;
===================

SET NULL "NULL";


SET SERVEROUTPUT ON;
SET FEEDBACK OFF;
SET ECHO OFF;
SET HEADING OFF;
SET WRAP OFF;
SET LINESIZE 10000;
SET TAB OFF;
SET PAGES 0;
SET DEFINE OFF;

DECLARE
-- Variables to store total salary and total employees
v_total_employees_active NUMBER := 0;
v_total_salary_active NUMBER := 0;
v_total_employees_terminated NUMBER := 0;
v_total_salary_terminated NUMBER := 0;

-- Cursor for active employees


CURSOR cur_active IS
SELECT NVL(dept_id, 0) AS dept_id,
COUNT(*) AS total_employees,
SUM(emp_sal) AS total_salary
FROM emp
WHERE emp_status = 'Active'
GROUP BY NVL(dept_id, 0)
ORDER BY NVL(dept_id, 0);

-- Cursor for terminated employees


CURSOR cur_terminated IS
SELECT NVL(dept_id, 0) AS dept_id,
COUNT(*) AS total_employees,
SUM(emp_sal) AS total_salary
FROM emp
WHERE emp_status = 'Terminated'
GROUP BY NVL(dept_id, 0)
ORDER BY NVL(dept_id, 0);

BEGIN
-- Display Active employee details
DBMS_OUTPUT.PUT_LINE('Details of Active employees');
DBMS_OUTPUT.PUT_LINE('-----------------------------------');
DBMS_OUTPUT.PUT_LINE('Department_id Total_employees Total_salary');

-- Process the active employees


FOR rec_active IN cur_active LOOP
DBMS_OUTPUT.PUT_LINE(rec_active.dept_id || ' ' || rec_active.total_employees
|| ' ' || rec_active.total_salary);

-- Accumulate totals for active employees


v_total_employees_active := v_total_employees_active +
rec_active.total_employees;
v_total_salary_active := v_total_salary_active + rec_active.total_salary;
END LOOP;

-- Display total employees and total salary for active employees


DBMS_OUTPUT.PUT_LINE('-----------------------------------');
DBMS_OUTPUT.PUT_LINE(v_total_employees_active || ' Total_employees');
DBMS_OUTPUT.PUT_LINE(v_total_salary_active || ' Total_salary');
DBMS_OUTPUT.PUT_LINE('-----------------------------------');

-- Display Terminated employee details


DBMS_OUTPUT.PUT_LINE('Details of Terminated employees');
DBMS_OUTPUT.PUT_LINE('-----------------------------------');
DBMS_OUTPUT.PUT_LINE('Department_id Total_employees Total_salary');

-- Process the terminated employees


FOR rec_terminated IN cur_terminated LOOP
DBMS_OUTPUT.PUT_LINE(rec_terminated.dept_id || ' ' ||
rec_terminated.total_employees || ' ' || rec_terminated.total_salary);

-- Accumulate totals for terminated employees


v_total_employees_terminated := v_total_employees_terminated +
rec_terminated.total_employees;
v_total_salary_terminated := v_total_salary_terminated +
rec_terminated.total_salary;
END LOOP;

-- Display total employees and total salary for terminated employees


DBMS_OUTPUT.PUT_LINE('-----------------------------------');
DBMS_OUTPUT.PUT_LINE(v_total_employees_terminated || ' Total_employees');
DBMS_OUTPUT.PUT_LINE(v_total_salary_terminated || ' Total_salary');
DBMS_OUTPUT.PUT_LINE('-----------------------------------');

END;
/

You might also like