当连接表中的列不为空时进行过滤

时间:2018-05-03 12:36:38

标签: sql postgresql join case postgresql-9.6

如果列不为空,是否有办法从连接表中有条件地过滤列?通过此查询,我得到了正确的结果:

with external_work_history as (
      select rr.*,
            rsa1.rsal_title as country,
            rsa2.rsal_title as function,
            rsa3.rsal_title as industry
      from (
            select
                  user_id,
                  unnest(string_to_array(resume_attribute_00069_1, ','))    as company,
                  unnest(string_to_array(resume_attribute_00071_13, ','))   as country_val_id,
                  unnest(string_to_array(resume_attribute_00067_18_2, ',')) as end_date,
                  unnest(string_to_array(resume_attribute_00070_13, ','))   as function_val_id,
                  unnest(string_to_array(resume_attribute_00068_1, ','))    as position,
                  unnest(string_to_array(resume_attribute_00067_18_1, ',')) as start_date,
                  unnest(string_to_array(resume_attribute_00072_13, ','))   as industry_val_id
            from import.vw_rpt_resume
      ) as rr
      left join import.vw_rpt_resume_section_attribute_value_local rsa1
        on rr.country_val_id = rsa1.rsal_value_id
      left join import.vw_rpt_resume_section_attribute_value_local rsa2
        on rr.function_val_id = rsa2.rsal_value_id
      left join import.vw_rpt_resume_section_attribute_value_local rsa3
        on rr.industry_val_id = rsa3.rsal_value_id
      left join corp_companyuserprofile cup
        on rr.user_id::int = cup.id
      where
        rsa1.culture_id::int = cup.language_id
        and (rsa2.culture_id::int = cup.language_id and not rsa2.rsal_title is null)
        and (rsa3.culture_id::int = cup.language_id and not rsa3.rsal_title is null)
)
   select * from external_work_history;

  user_id | company | country_val_id | end_date | function_val_id | position | start_date | industry_val_id | country | function | industry 
---------+---------+----------------+----------+-----------------+----------+------------+-----------------+---------+----------+----------
(0 rows)

'industry'列为null,如果我在该列上有where条件:

<select (change)="your_method(values[$event.target.selectedIndex])">
  <option *ngFor="let v of values" [value]="v" >  
    {{v.name}}
  </option>
</select>

我没有结果。

我需要的是,对于WHERE子句中的每个条件,如果'rsal_title'列不为空,则执行过滤。

也许我正在使用错误的连接类型,或者只是在where子句中添加case语句,对此有何帮助?

1 个答案:

答案 0 :(得分:0)

@eurotrash

感谢您的回复,但是我把错误放在WHERE子句中是错误的,而在ON连接条件中则需要:

with external_work_history as (
      select rr.*,
            rsa1.rsal_title as country,
            rsa2.rsal_title as function,
            rsa3.rsal_title as industry
      from (
            select
                  user_id,
                  unnest(string_to_array(resume_attribute_00069_1, ','))    as company,
                  unnest(string_to_array(resume_attribute_00071_13, ','))   as country_val_id,
                  unnest(string_to_array(resume_attribute_00067_18_2, ',')) as end_date,
                  unnest(string_to_array(resume_attribute_00070_13, ','))   as function_val_id,
                  unnest(string_to_array(resume_attribute_00068_1, ','))    as position,
                  unnest(string_to_array(resume_attribute_00067_18_1, ',')) as start_date,
                  unnest(string_to_array(resume_attribute_00072_13, ','))   as industry_val_id
            from import.vw_rpt_resume
      ) as rr
      left join corp_companyuserprofile cup
        on rr.user_id::int = cup.id
      left join import.vw_rpt_resume_section_attribute_value_local rsa1
        on rr.country_val_id = rsa1.rsal_value_id and rsa1.culture_id::int = cup.language_id
      left join import.vw_rpt_resume_section_attribute_value_local rsa2
        on rr.function_val_id = rsa2.rsal_value_id and rsa2.culture_id::int = cup.language_id
      left join import.vw_rpt_resume_section_attribute_value_local rsa3
        on rr.industry_val_id = rsa3.rsal_value_id and rsa3.culture_id::int = cup.language_id
)
   select * from external_work_history;