简体   繁体   English

在使用左外连接的 SQL 查询中需要帮助

[英]Need help in SQL query with left outer join

I am trying to get the employee's details who have taken leaves and who haven't taken leaves using the below query, but it is not returning the employee's names who have not taken any leaves.我正在尝试使用以下查询获取已请假和未请假的员工的详细信息,但它不会返回未请假的员工姓名。

Any help would be appreciated.任何帮助,将不胜感激。

SELECT distinct paf.employee_number,PAF.FULL_NAME,aba.absence_attendance_type_id
           FROM 
                per_all_assignments_f ass,
                per_all_people_f paf,
                per_absence_attendances aba
          WHERE     1 = 1
          AND ass.assignment_number  = paf.employee_number
          
                AND aba.person_id(+) = ass.person_id
                AND aba.absence_attendance_type_id =28074
                AND ass.business_group_id = 134
                AND TRUNC (SYSDATE) BETWEEN TRUNC (ass.effective_start_date)
                                        AND TRUNC (ass.effective_end_date)
                AND TRUNC (SYSDATE) BETWEEN TRUNC (paf.effective_start_date)
                                        AND  nvl(paf.effective_end_date, trunc(sysdate))    
                and paf.current_employee_flag = 'Y'
                order by paf.employee_number;

Please try this:请试试这个:

SELECT distinct paf.employee_number,PAF.FULL_NAME,aba.absence_attendance_type_id
       FROM 
            per_all_assignments_f ass 
            inner join per_all_people_f paf on ass.assignment_number  = paf.employee_number
            Left Join per_absence_attendances aba on aba.person_id = ass.person_id
            and aba.absence_attendance_type_id =28074
      WHERE               
            ass.business_group_id = 134
            AND TRUNC (SYSDATE) BETWEEN TRUNC (ass.effective_start_date)
                                    AND TRUNC (ass.effective_end_date)
            AND TRUNC (SYSDATE) BETWEEN TRUNC (paf.effective_start_date)
                                    AND  nvl(paf.effective_end_date, trunc(sysdate))    
            and paf.current_employee_flag = 'Y'
            order by paf.employee_number;

Thanks for helping me..谢谢你帮助我。。

I have used below query to get all the employee's name我使用下面的查询来获取所有员工的姓名

SELECT distinct paf.employee_number,PAF.FULL_NAME,aba.absence_attendance_type_id
       FROM 
            per_all_assignments_f ass,
            per_all_people_f paf,
            per_absence_attendances aba
      WHERE     1 = 1
      AND ass.assignment_number  = paf.employee_number
      
            AND aba.person_id(+) = ass.person_id
            AND aba.absence_attendance_type_id (+)= 28074
            AND ass.business_group_id = 134
            AND TRUNC (SYSDATE) BETWEEN TRUNC (ass.effective_start_date)
                                    AND TRUNC (ass.effective_end_date)
            AND TRUNC (SYSDATE) BETWEEN TRUNC (paf.effective_start_date)
                                    AND  nvl(paf.effective_end_date, trunc(sysdate))    
            and paf.current_employee_flag = 'Y'
            order by paf.employee_number;

Adding (+) in this line "aba.absence_attendance_type_id (+)= 28074" worked for me.在“aba.absence_attendance_type_id (+)= 28074”这一行中添加 (+) 对我有用。

And the query provided by @Kazi Mohammad Ali Nur is also working. @Kazi Mohammad Ali Nur 提供的查询也有效。 Thank you again.再次感谢你。 Now I understood the issue and the use of left outer join specially when multiple conditions are required to filter the data.现在我了解了这个问题以及在需要多个条件来过滤数据时特别使用左外连接。

Sample Output:样品 Output:

Emp no员工编号 Name姓名 Attendence Id出勤编号
1 1 John约翰 123 123
2 2 Poly 123 123
3 3 Sopra索普拉
4 4 Cali卡利 123 123

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM