Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

Permintaan untuk menemukan Karyawan yang telah mengambil lebih dari cuti yang memenuhi syarat sehubungan dengan Peran Pekerjaan mereka

Ini akan menemukan setiap pengguna yang telah melebihi jumlah cuti untuk setiap jenis cuti:

SQL Fiddle

Pengaturan Skema 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;

Kueri 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

Hasil :

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



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Oracle ODP.Net dan EF CodeFirst - Kesalahan SaveChanges

  2. Oracle OSM - XQuery tidak berulang

  3. Bagaimana jumlah dengan pernyataan kondisional kasus bekerja di sql

  4. Bagaimana cara mengekspor hasilnya ke berbagai tab Excel di Toad for Data Analyst?

  5. Konversi skrip Coldfusion dalam tampilan Oracle dengan CASE dan iterasi