Това ще намери всеки потребител, който е превишил размера на отпуска за всеки тип отпуск:
Настройка на схема на Oracle 11g R2 :
СЪЗДАВАНЕ НА ТАБЛИЦА Employee24( EMPLOYEEID, JOBROLES ) ASSELECT 1, 'RoleA' FROM DUAL UNION ALLSELECT 2, 'RoleB' FROM DUAL UNION ALLSELECT 3, 'RoleB' FROM DUAL;CREATE TABLE Leave25( EMPLOYEEID,LEAVEID, LEAVETYPE , STARTDATE, ENDDATE) ASSELECT 1,1,'SickLeave', DATE '2018-01-01', DATE '2018-01-11' FROM DUAL UNION ALLSELECT 1,2,'SickLeave', DATE '2018-01-21 ', ДАТА '2018-01-31' FROM DUAL UNION ALLSELECT 1,3,'EarnedLeave',DATE '2018-01-11', DATE '2018-01-21' FROM DUAL UNION ALLSELECT 1,4,'EarnedLeave' ,ДАТА '2018-02-01', ДАТА '2018-02-11' ОТ DUAL UNION ALLSELECT 1,5,'EarnedLeave',DATE '2018-02-21', ДАТА '2018-03-03' ОТ DUAL UNION ALLSELECT 2,6,'EarnedLeave',DATE '2018-02-01', DATE '2018-02-13' FROM DUAL UNION ALLSELECT 3,7,'SickLeave', DATE '2018-01-01', DATE '2018 -01-09' FROM DUAL;CREATE TABLE JOBROLESELIGIBLELE(JOBROLES, ELIGIBLE_SICK_LEAVES, ELIGIBLE_EARNED_LEAVES) ASSELECT 'RoleA', 14, 24 FROM DUAL UNION ALLSELECT 'RoleB', 7, 10 FROM DUAL;
Заявка 1 :
SELECT e.employeeId, l.leavetype, l.days_leave, r.AllowedLeaveAmountFROM 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.reavetype AND jobroles =r.jobroles )WHERE l.days_leave> r.AllowedLeaveAmount
<предварителен код>| СЛУЖИТЕЛ | ОТПУСК | DAYS_LEAVE | РАЗРЕШЕН ОТПУСК ||------------|-------------|------------|------- -------------|| 1 | Отпуск по болест | 20 | 14 || 1 | Заслужен отпуск | 30 | 24 || 2 | Заслужен отпуск | 12 | 10 || 3 | Отпуск по болест | 8 | 7 |