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

后端 未结 2 873
离开以前
离开以前 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: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;
    

提交回复
热议问题