查询以显示员工姓名,离开的叶子,符合条件的叶子和百分比

时间:2018-05-30 12:11:03

标签: sql oracle oracle11g

我将这些表格包含以下列:

   Employee24( EMPLOYEEID, FIRSTNAME, LASTNAME, GENDER, JOBROLES);

   Leave25( EMPLOYEEID,LEAVEID, LEAVETYPE, STARTDATE, ENDDATE);

  JOBROLESELIGIBLELE(JOBROLES, ELIGIBLE_SICK_LEAVES, ELIGIBLE_EARNED_LEAVES)

我想写一个查询来显示员工的名字,离开的叶子,有资格的叶子和百分比

        SELECT * 
  FROM EMPLOYEE24 
  WHERE EMPLOYEEID IN (SELECT EMPLOYEEID 
                        FROM LEAVE25 L WHERE (STARTDATE-ENDDATE) AS "TAKENLEAVE"
    < (SELECT (ELIGIBLE_SICK_LEAVES + ELIGIBLE_EARNED_LEAVES) AS "ELIGIBLELEAVE"
              ,TAKENLEAVE/ELIGIBLELEAVE * 100
              FROM  JOBROLESELIGIBELE));

示例数据:            CREATE TABLE Employee24(EMPLOYEEID,JOBROLES)AS            选择1,'RoleA'来自DUAL UNION ALL            选择2,'RoleB'来自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;

预期结果:       EMPLOYEEID | FIRSTNAME | LASTNAME | LEAVESTAKEN | ELIGIBLELEAVES |百分比

2 个答案:

答案 0 :(得分:1)

对我previous answer的一个小改动:

SQL Fiddle

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 UNION ALL
SELECT 4, 'RoleA' FROM DUAL UNION ALL
SELECT 5, '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 UNION ALL
SELECT 5,8,'EarnedLeave',DATE '2018-02-01', DATE '2018-02-11' 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;

查询1

SELECT e.employeeId,
       r.leavetype,
       COALESCE( l.days_leave, 0 ) AS days_leave,
       r.AllowedLeaveAmount,
       100 * COALESCE( l.days_leave, 0 ) / r.AllowedLeaveAmount As leave_percent 
FROM   Employee24 e
       INNER JOIN
       ( SELECT *
         FROM   JobRolesEligibleLE
         UNPIVOT ( AllowedLeaveAmount FOR LeaveType IN (
           Eligible_Sick_Leaves   AS 'SickLeave',
           Eligible_Earned_Leaves AS 'EarnedLeave'
         ) )
       ) r
       ON ( e.jobroles   = r.jobroles )
       LEFT OUTER JOIN
       ( SELECT employeeId,
                SUM( enddate - startdate ) AS days_leave,
                leavetype
         FROM   Leave25
         GROUP BY employeeId, leaveType
       ) l
       ON (   e.employeeId = l.employeeId
          AND l.leavetype = r.leavetype )
ORDER BY employeeId, LeaveType

<强> Results

| EMPLOYEEID |   LEAVETYPE | DAYS_LEAVE | ALLOWEDLEAVEAMOUNT |      LEAVE_PERCENT |
|------------|-------------|------------|--------------------|--------------------|
|          1 | EarnedLeave |         30 |                 24 |                125 |
|          1 |   SickLeave |         20 |                 14 | 142.85714285714286 |
|          2 | EarnedLeave |         12 |                 10 |                120 |
|          2 |   SickLeave |          0 |                  7 |                  0 |
|          3 | EarnedLeave |          0 |                 10 |                  0 |
|          3 |   SickLeave |          8 |                  7 | 114.28571428571429 |
|          4 | EarnedLeave |          0 |                 24 |                  0 |
|          4 |   SickLeave |          0 |                 14 |                  0 |
|          5 | EarnedLeave |         10 |                 10 |                100 |
|          5 |   SickLeave |          0 |                  7 |                  0 |

答案 1 :(得分:0)

您不能在列列表中使用别名来处理列,您必须使用完整表达式。并且您不能在WHERE子句中的列上引入别名。

也许你想要这样的东西:

SELECT E.FIRSTNAME,
       E.LASTNAME,
       (SELECT SUM(L.STARTDATE - L.ENDDATE)
               FROM LEAVE25 L
               WHERE L.EMPLOYEEID = E.EMPLOYEEEID) "TAKENLEAVE",
       J.ELIGIBLE_SICK_LEAVES + J.ELIGIBLE_EARNED_LEAVES "ELIGIBLELEAVE",
       (SELECT SUM(L.STARTDATE - L.ENDDATE)
               FROM LEAVE25 L
               WHERE L.EMPLOYEEID = E.EMPLOYEEEID)
       /
       (J.ELIGIBLE_SICK_LEAVES + J.ELIGIBLE_EARNED_LEAVES)
       * 100 "PERCENTAGE"
       FROM EMPLOYEE24 E
            LEFT JOIN JOBROLESELIGIBLELE J
                      ON J.JOBROLES = E.JOBROLES;