简体   繁体   中英

Left outer join on a function in SQL query

I have a column assignment number which can have numbers like 1298287 , 82727-2 , 8367-3,7363 etc

I am using regex to eliminate-2 -3 from these numbers to retrieve person number. Now after this is eliminated I need to compare this person number with the person number in another table.

When I am using join for this it is retrieving less number of rows. I have 13000 rows in table xx_ass_table and I need employer name and worker type from the work_stg table for which I need to join this regex person number with person number from work_stg .

select 
    assignment_name,
    regexp_substr(assignment_name, '[0-9]+') person_number,
    wrk.employer_name ,
    wrk.TIME,
    STEP_ENTRY_DATE ,
    SYSTEM_PERSON_TYPE ,
    WORK_ATHOME_FLAG ,
    worker_category,
    effective_start_date,
    effective_end_date,
from  
    (select
         xx_ass_table.*,
         COUNT(*) OVER (PARTITION BY assignment_name, 
                                     effective_start_date,
                                     effective_end_date,
                                     effective_latest_change) as c
     from xx_ass_table) t,
    xx_work_stg wrk
where 
    regexp_substr(t.assignment_name(+), '[0-9]+') = wrk.person_number
    and c = 1;

The table xx_ass_table has few person numbers which are not ib work relationship table that is why I am using left join. But this query is returning 4000 rows, when it should return 13k rows.

Try avoid this kind of joining and use the correct syntax of a left outer join, I'm not sure you placed your PLUS sign correctly. Try this:

select assignment_name,
    regexp_substr(assignment_name, '[0-9]+') person_number,
    wrk.employer_name ,
    wrk.TIME,
    STEP_ENTRY_DATE ,
    SYSTEM_PERSON_TYPE ,
    WORK_ATHOME_FLAG ,
    worker_category,
    effective_start_date,
    effective_end_date,

  from  (SELECT xx_ass_table.*,
           COUNT(*) OVER (PARTITION BY assignment_name, 
                                      effective_start_date,
                                      effective_end_date,
                                      effective_latest_change) as c
    from   xx_ass_table) t
 left outer join 
    xx_work_stg wrk
on regexp_substr(t.assignment_name, '[0-9]+')=wrk.person_number

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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