如何在“ IN条件”中包含Null

时间:2019-11-16 03:37:52

标签: sql oracle

我想过滤那些具有action_type且值为'Q','R'的员工,或者它可以为null,即没有action_type。我使用了旧的SQL语法,因为这是我公司遵循的方法。

我在查询中也使用了“ Union”,以便在输出中也包含null。但我认为可以有更好,更充分的方法来做到这一点 条件是:

 AND ppa.action_type IN ( 'Q', 'R' ) 

SELECT * 
FROM   (SELECT DISTINCT papf.person_number, 
                        name.first_name, 
                        name.last_name, 
                        paam.assignment_number, 
                        py.payroll_name, 
                        (SELECT meaning 
                         FROM   fnd_lookup_values 
                         WHERE  lookup_type LIKE 'ACTION_TYPE' 
                                AND lookup_code = ppa.action_type 
                                AND LANGUAGE = 'US' 
                                AND enabled_flag = 'Y')            ACTION_TYPE, 
                        To_char(PPA.effective_date, 'DD-MON-YYYY') 
                        EFFECTIVE_DATE, 
                        ppa.payroll_action_id                      action_number 
                        , 
                        pcs.consolidation_set_name, 
                        To_char(finc, 'DD-MON-YYYY')               FINC, 
                        pasv.user_status
        FROM   pay_assigned_payrolls_dn papd, 
               pay_payroll_terms pt, 
               pay_pay_relationships_dn pr, 
               pay_all_payrolls_f py, 
               per_all_people_f papf, 
               pay_payroll_actions ppa, 
               pay_consolidation_sets pcs, 
               per_person_names_f name, 
               pay_rel_groups_dn payrel, 
               per_all_assignments_m paam, 
               pay_payroll_rel_actions PPRA, 
               per_assignment_status_types_vl pasv, 
               pay_time_periods ptp, 
               per_legislative_data_groups_vl ldg 
        WHERE  papd.payroll_term_id = pt.payroll_term_id 
               AND pt.payroll_relationship_id = pr.payroll_relationship_id 
               AND papd.payroll_id = py.payroll_id 
               AND pr.person_id = papf.person_id 
               AND ppa.payroll_id(+) = py.payroll_id 
               AND ptp.payroll_id = py.payroll_id 
               AND PPA.payroll_action_id(+) = PPRA.payroll_action_id 
               AND PPRA.payroll_relationship_id = pr.payroll_relationship_id 
               AND pasv.assignment_status_type_id = 
                   paam.assignment_status_type_id 
               AND paam.assignment_type = 'E' 
               --ADDED condition---   
               AND pasv.user_status LIKE '%Payroll Eligible' 
               AND ( finc > ptp.start_date 
                      OR finc IS NULL ) 
               AND ppa.action_type IN ( 'Q', 'R' ) 
               AND ldg.name = Nvl(:P_LEGISLATIVE_GROUP, ldg.name) 
               AND pasv.user_status = Nvl(:P_ASSIGNMENT_STATUS, 
                                      pasv.user_status) 
               AND To_char(Nvl(ppa.effective_date, SYSDATE), 'YYYY-MM-DD') 
                   BETWEEN 
                   To_char(Nvl(:p_from_date, SYSDATE), 'YYYY-MM-DD') AND To_char 
                   ( 
                       Nvl(:p_TO_date, SYSDATE), 'YYYY-MM-DD') 
               -------   
               AND ldg.legislation_code = paam.legislation_code 
               AND ppa.consolidation_set_id(+) = pcs.consolidation_set_id 
               AND pcs.consolidation_set_id = py.consolidation_set_id 
               AND payrel.assignment_id = paam.assignment_id 
               AND payrel.group_type = 'A' 
               AND papf.person_id = name.person_id 
               AND name.name_type = 'GLOBAL' 
               AND payrel.parent_rel_group_id = papd.payroll_term_id 
               AND ppa.earn_time_period_id(+) = ptp.time_period_id 
               AND Nvl(ppa.effective_date, Trunc(SYSDATE)) BETWEEN 
                   payrel.start_date AND payrel.end_date 
               AND Nvl(ppa.effective_date, Trunc(SYSDATE)) BETWEEN 
                   papd.start_date AND papd.end_date 
               AND Trunc(SYSDATE) BETWEEN papf.effective_start_date AND 
                                          papf.effective_end_date 
               AND Trunc(SYSDATE) BETWEEN py.effective_start_date AND 
                                          py.effective_end_date 
               AND Trunc(SYSDATE) BETWEEN paam.effective_start_date AND 
                                          paam.effective_end_date 
               AND Trunc(SYSDATE) BETWEEN name.effective_start_date AND 
                                          name.effective_end_date 
        UNION 
        SELECT DISTINCT papf.person_number, 
                        name.first_name, 
                        name.last_name, 
                        paam.assignment_number, 
                        py.payroll_name, 
                        (SELECT meaning 
                         FROM   fnd_lookup_values 
                         WHERE  lookup_type LIKE 'ACTION_TYPE' 
                                AND lookup_code = ppa.action_type 
                                AND LANGUAGE = 'US' 
                                AND enabled_flag = 'Y')            ACTION_TYPE, 
                        To_char(PPA.effective_date, 'DD-MON-YYYY') 
                        EFFECTIVE_DATE, 
                        ppa.payroll_action_id                      action_number 
                        , 
                        pcs.consolidation_set_name, 
                        To_char(finc, 'DD-MON-YYYY')               FINC, 
                        pasv.user_status
        FROM   pay_assigned_payrolls_dn papd, 
               pay_payroll_terms pt, 
               pay_pay_relationships_dn pr, 
               pay_all_payrolls_f py, 
               per_all_people_f papf, 
               pay_payroll_actions ppa, 
               pay_consolidation_sets pcs, 
               per_person_names_f name, 
               pay_rel_groups_dn payrel, 
               per_all_assignments_m paam, 
               pay_payroll_rel_actions PPRA, 
               per_assignment_status_types_vl pasv, 
               pay_time_periods ptp, 
               per_legislative_data_groups_vl ldg 
        WHERE  papd.payroll_term_id = pt.payroll_term_id 
               AND pt.payroll_relationship_id = pr.payroll_relationship_id 
               AND papd.payroll_id = py.payroll_id 
               AND pr.person_id = papf.person_id 
               AND ppa.payroll_id(+) = py.payroll_id 
               AND ptp.payroll_id = py.payroll_id 
               AND PPA.payroll_action_id(+) = PPRA.payroll_action_id 
               AND PPRA.payroll_relationship_id = pr.payroll_relationship_id 
               AND pasv.assignment_status_type_id = 
                   paam.assignment_status_type_id 
               AND paam.assignment_type = 'E' 
               --ADDED condition---   
               AND pasv.user_status LIKE '%Payroll Eligible' 
               AND ( finc > ptp.start_date 
                      OR finc IS NULL ) 
               AND ppa.action_type IS NULL 
               AND ldg.name = Nvl(:P_LEGISLATIVE_GROUP, ldg.name) 
               AND pasv.user_status = Nvl(:P_ASSIGNMENT_STATUS, 
                                      pasv.user_status) 
               AND To_char(Nvl(ppa.effective_date, SYSDATE), 'YYYY-MM-DD') 
                   BETWEEN 
                   To_char(Nvl(:p_from_date, SYSDATE), 'YYYY-MM-DD') AND To_char 
                   ( 
                       Nvl(:p_TO_date, SYSDATE), 'YYYY-MM-DD') 
               -------   
               AND ldg.legislation_code = paam.legislation_code 
               AND ppa.consolidation_set_id(+) = pcs.consolidation_set_id 
               AND pcs.consolidation_set_id = py.consolidation_set_id 
               AND payrel.assignment_id = paam.assignment_id 
               AND payrel.group_type = 'A' 
               AND papf.person_id = name.person_id 
               AND name.name_type = 'GLOBAL' 
               AND payrel.parent_rel_group_id = papd.payroll_term_id 
               AND ppa.earn_time_period_id(+) = ptp.time_period_id 
               AND Nvl(ppa.effective_date, Trunc(SYSDATE)) BETWEEN 
                   payrel.start_date AND payrel.end_date 
               AND Nvl(ppa.effective_date, Trunc(SYSDATE)) BETWEEN 
                   papd.start_date AND papd.end_date 
               AND Trunc(SYSDATE) BETWEEN papf.effective_start_date AND 
                                          papf.effective_end_date 
               AND Trunc(SYSDATE) BETWEEN py.effective_start_date AND 
                                          py.effective_end_date 
               AND Trunc(SYSDATE) BETWEEN paam.effective_start_date AND 
                                          paam.effective_end_date 
               AND Trunc(SYSDATE) BETWEEN name.effective_start_date AND 
                                          name.effective_end_date)

3 个答案:

答案 0 :(得分:3)

有两种方法:

AND (action_type IN ('Q', 'R') OR action_type IS NULL)

或:

AND COALESCE(action_type, 'Q') IN ('Q', 'R')

根据您的具体情况,两者之间的性能可能会有所不同,也可能会有所不同。

答案 1 :(得分:2)

or怎么样?

(ppa.action_type IN ( 'Q', 'R' ) or pa.action_type is null)

答案 2 :(得分:0)

您可以考虑简要地使用Oracle spesific NVL函数NVL( value1, value2 ) replace value1 with value2 if value1 is null

  

NVL( action_type, 'Q') IN ( 'Q', 'R' )

如果action_type也为null,则NVL( action_type, 'Q')与括号内列表中存在的'Q'匹配。