Oracle
 sql >> Database >  >> RDS >> Oracle

Interrogare per trovare i dipendenti che hanno preso più del loro congedo idoneo rispetto ai loro ruoli di lavoro

Questo troverà ogni utente che ha superato l'importo del congedo per ogni tipo di congedo:

SQL Fiddle

Impostazione dello schema Oracle 11g R2 :

CREATE TABLE Employee24( EMPLOYEEID, JOBROLES ) AS
SELECT 1, 'RoleA' FROM DUAL UNION ALL
SELECT 2, 'RoleB' FROM DUAL UNION ALL
SELECT 3, 'RoleB' FROM DUAL;

CREATE TABLE Leave25( EMPLOYEEID,LEAVEID, LEAVETYPE, STARTDATE, ENDDATE) AS
SELECT 1,1,'SickLeave',  DATE '2018-01-01', DATE '2018-01-11' FROM DUAL UNION ALL
SELECT 1,2,'SickLeave',  DATE '2018-01-21', DATE '2018-01-31' FROM DUAL UNION ALL
SELECT 1,3,'EarnedLeave',DATE '2018-01-11', DATE '2018-01-21' FROM DUAL UNION ALL
SELECT 1,4,'EarnedLeave',DATE '2018-02-01', DATE '2018-02-11' FROM DUAL UNION ALL
SELECT 1,5,'EarnedLeave',DATE '2018-02-21', DATE '2018-03-03' FROM DUAL UNION ALL
SELECT 2,6,'EarnedLeave',DATE '2018-02-01', DATE '2018-02-13' FROM DUAL UNION ALL
SELECT 3,7,'SickLeave',  DATE '2018-01-01', DATE '2018-01-09' FROM DUAL;


CREATE TABLE JOBROLESELIGIBLELE(JOBROLES, ELIGIBLE_SICK_LEAVES, ELIGIBLE_EARNED_LEAVES) AS
SELECT 'RoleA', 14, 24 FROM DUAL UNION ALL
SELECT 'RoleB',  7, 10 FROM DUAL;

Query 1 :

SELECT e.employeeId,
       l.leavetype,
       l.days_leave,
       r.AllowedLeaveAmount
FROM   Employee24 e
       INNER JOIN
       ( SELECT employeeId,
                SUM( enddate - startdate ) AS days_leave,
                leavetype
         FROM   Leave25
         GROUP BY employeeId, leaveType
       ) l
       ON ( e.employeeId = l.employeeId )
       INNER JOIN
       ( SELECT *
         FROM   JobRolesEligibleLE
         UNPIVOT ( AllowedLeaveAmount FOR LeaveType IN (
           Eligible_Sick_Leaves   AS 'SickLeave',
           Eligible_Earned_Leaves AS 'EarnedLeave'
         ) )
       ) r
       ON (    l.leavetype = r.leavetype
           AND e.jobroles   = r.jobroles )
WHERE  l.days_leave > r.AllowedLeaveAmount

Risultati :

| EMPLOYEEID |   LEAVETYPE | DAYS_LEAVE | ALLOWEDLEAVEAMOUNT |
|------------|-------------|------------|--------------------|
|          1 |   SickLeave |         20 |                 14 |
|          1 | EarnedLeave |         30 |                 24 |
|          2 | EarnedLeave |         12 |                 10 |
|          3 |   SickLeave |          8 |                  7 |