How to join 2 queries with different number of records and columns in oracle sql?

后端 未结 2 864
离开以前
离开以前 2021-01-29 07:56

I have three tables:

Employee_leave(EmployeeID,Time_Period,leave_type)  
Employee(EID,Department,Designation) 
 leave_eligibility(Department,Designation, LeaveTy         


        
相关标签:
2条回答
  • 2021-01-29 08:12

    To get the final desired output ...

    "For a record which is not present in output of query1, it should display 0 in final output. "

    ... use an outer join to tie the taken leave records to the other tables. This will give zero time_duration for leave types which the employee has not taken.

    select emp.Employee_ID
           , le.leavetype
           , le.leavebalance 
           , sum (el.Time_Duration) as total_Time_Duration
    from employee emp
         inner join leave_eligibility le
              on le.department= emp.department 
                 and le.designation= emp.designation 
         left outer join Employee_leave el
               on el.EmployeeID = emp.Employee_ID
               and el.leave_type = le.leavetype        
    group by emp.Employee_ID
           , le.leavetype
           , le.leavebalance 
           ;
    

    Your immediate problem:

    I'm getting "%s: invalid identifier"

    Your view has references to a column EID although none of your posted tables have a column of that name. Likewise there is confusion between Time_Duration and time_period.

    More generally, you will find life considerably easier if you use the exact same name for common columns (i.e. consistently use either employee_id or employeeid, don't chop and change).

    0 讨论(0)
  • 2021-01-29 08:34

    Try this examle:

    with t as (
        select 'Casual' as Leave_Type, 1 as Time_Period, 0 as LeaveBalance from dual
        union all
        select 'Paid', 4,0 from dual
        union all
        select 'Sick', 1,0 from dual),
    t1 as (
        select 'Casual' as Leave_Type, 0 as Time_Period, 10 as LeaveBalance from dual
        union all
        select 'Paid', 0, 15 from dual
        union all
        select 'Privlage', 0, 6 from dual
        union all
        select 'Sick', 0, 20 from dual)
    
    select Leave_Type, sum(Time_Period), sum(LeaveBalance) 
    from(
        select *
        from t
        UNION ALL
        select * from t1
    ) 
    group by Leave_Type 
    

    Ok, edit:

    create or replace view combo_table1 as 
    Select UNIQUE Leavetype, 0 AS Leave_Availed, LEAVEBALANCE 
    from LEAVE_ELIGIBILITY INNER JOIN EMPLOYEE ON LEAVE_ELIGIBILITY.DEPARTMENT= EMPLOYEE.DEPARTMENT AND LEAVE_ELIGIBILITY.DESIGNATION= EMPLOYEE.DESIGNATION 
    WHERE EID='78';
    
    create or replace view combo_table2 as 
    SELECT LEAVE_TYPE as Leavetype, SUM(TIME_PERIOD) AS Leave_Availed, 0 as LEAVEBALANCE 
    FROM EMPLOYEE_LEAVE 
    WHERE EMPLOYEEID='78'
    GROUP BY LEAVE_TYPE, LEAVEBALANCE;
    
    SELECT Leavetype, sum(LEAVEBALANCE), sum(leave_availed)
    FROM (
      select *
        from combo_table1
        UNION ALL
        select * from combo_table2
    )
    group by Leavetype;
    
    0 讨论(0)
提交回复
热议问题