Фильтрация, когда столбец из объединенной таблицы не равен NULL - PullRequest
0 голосов
/ 03 мая 2018

Есть ли способ условно фильтровать столбец из объединенной таблицы, если столбец не нулевой? С этим запросом у меня правильные результаты:

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 
---------+------------------------+----------------+------------+-----------------+--------------------------------------------------+------------+-----------------+--------------------------+--------------------+----------
 9       | FFF Inc                | 317            | 2013-07-31 | 322             | Tech Consulant                                   | 2010-02-01 |                 | Spain                    | Quality            | 
 9       | YYY SRL                | 288            | 2014-12-31 | 321             | BA                                               | 2013-08-01 |                 | United States of America | General Management | 
 43      | comp 1                 | 313            | 11/11/2009 | 283             | Senior Consultant                                | 04/04/2003 |                 | Australia                | Finance            | 
 43      | Company 11             | 312            | 12/31/2012 | 283             | Manager                                          | 12/01/2009 |                 | Taiwan                   | Finance            | 
 43      | WWWRRR                 | 311            | 03/03/2003 | 283             | Junior Consultant                                | 02/02/1999 |                 | Angola                   | Finance            | 
 541     | bla bla Hotel          | 318            | 08/31/2011 | 321             | Branch Manager                                   | 01/01/2008 |                 | Poland                   | General Management | 
 541     | SSS                    | 317            | 07/09/2013 | 323             | Marketing Manager                                | 12/07/2011 |                 | Spain                    | Marketing          | 
 613     | Company 2              | 287            | 2008-03-28 | 322             | LABORATORY SAFETY OFFICER AND RESEARCH ASSISTANT | 2006-08-21 |                 | France                   | Quality            | 
 613     | Company 2              | 287            | 2006-08-18 | 322             | LABORATORY TECHNOLOGIST                          | 2004-06-07 |                 | France                   | Quality            | 
 613     | Company AAA            | 286            | 2004-05-25 | 320             | Oncology Intern                                  | 2003-07-07 |                 | Germany                  | Research           | 
 603     | Corp 3                 | 288            | 2016-06-30 | 320             | Medical Engineer                                 | 2015-01-01 |                 | United States of America | Research           | 
 603     | Corp 4                 | 286            | 2014-12-31 | 320             | Project Manager                                  | 2011-01-03 |                 | Germany                  | Research           | 
 564     | Company 11             | 316            | 12/31/2013 | 322             | Senior Manager - Lab                             | 06/01/2009 |                 | Slovenia                 | Quality            | 
 564     | QQQ comp               | 316            | 05/31/2009 | 322             | Senior Lab Analyst                               | 02/01/2001 |                 | Slovenia                 | Quality            | 

столбец 'промышленность' равен нулю, если у меня было условие where для этого столбца:

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)

У меня нет результатов.

Мне нужно выполнить для каждого условия в предложении WHERE фильтрацию, если столбец 'rsal_title' не равен NULL.

Может быть, я использую неправильный тип соединения, или это просто вопрос добавления оператора case в предложении where, какая-нибудь помощь по этому поводу?

1 Ответ

0 голосов
/ 03 мая 2018

@ 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;
...