postgres - 具有动态列值的横向查询

时间:2018-04-30 20:46:44

标签: sql postgresql subquery common-table-expression postgresql-9.6

我需要横向子查询的帮助:

是否可以使用动态生成的列名执行它?

我尝试过,但我做错了:

-- table containing column names as values
nov=# select rsi.display_field from corp_resumesectionitem rsi limit 15;
        display_field        
-----------------------------
 resume_attribute_00064_13
 resume_attribute_00065_13
 resume_attribute_00066_13
 resume_attribute_00067_18_1
 resume_attribute_00067_18_2
 resume_attribute_00068_1
 resume_attribute_00069_1
 resume_attribute_00070_13
 resume_attribute_00071_13
 resume_attribute_00072_13
 resume_attribute_00082_1
 resume_attribute_00083_1
 resume_attribute_00084_6
 resume_attribute_00085_13
 resume_attribute_00086_3
(15 rows)

-- import.vw_rpt_resume table contains several colum with names corresponding to the display_field values, such as 'resume_attribute_00064_13', 'resume_attribute_00065_13':

nov=# select column_name from information_schema.columns where table_name='vw_rpt_resume';                                                                                                                                                              column_name             
------------------------------------
 user_id
 warehouse_resume_attribute_user_id
 resume_attribute_000m1_2
 resume_attribute_00032_13
 resume_attribute_00052_13
 resume_attribute_00053_13
 resume_attribute_00057_4
 resume_attribute_00058_7
 resume_attribute_00059_6
 resume_attribute_00061_3
 ...
 ...
 resume_attribute_00094_18_1
 resume_attribute_00094_18_2
 resume_attribute_00095_1
 resume_attribute_00096_13
 resume_attribute_00097_13
 resume_attribute_00098_7
 resume_attribute_00099_2
 resume_attribute_00100_13
 resume_attribute_00101_13
 resume_attribute_00102_13
(55 rows)

我需要在LATERAL子查询中使用动态列名执行查询,不要搞砸了......

之类的东西(这更像是伪代码,但它仅仅是为了提出一个想法):

select * from (
  select
    rsi.display_field as df,
    subq.*
  from corp_resumesectionitem rsi, lateral (
    execute 'select user_id, unnest(string_to_array(' || rsi.display_field '' ', ',')) as val, ' || rsi.display_field || ' as col_name from import.vw_rpt_resume subq'
)

-- but this is not working...

每个横向查询都应该是这样的:

select * from (
    select distinct r.user_id, r.val, m.name_display, l.rsal_value_id, l.rsal_title, m.display_field, l.culture_id
    from (
    select user_id,
      unnest(string_to_array(resume_attribute_00032_13, ',')) as val,
      'resume_attribute_00032_13' as col_name from import.vw_rpt_resume
    ) as r
  inner join import.custom_fields_mapping m
    on r.col_name::text = m.display_field
  inner join import.vw_rpt_resume_section_attribute_value_local as l
  on r.val = l.rsal_value_id) as dd;

结果像这样的结果:

user_id | val |              name_display               | rsal_value_id |   rsal_title    |       display_field       | culture_id 
---------+-----+-----------------------------------------+---------------+-----------------+---------------------------+------------
56      | 226 | Skills & Knowledge - Skills & Knowledge | 226           | Being resilient | resume_attribute_00032_13 | 1
56      | 226 | Skills & Knowledge - Skills & Knowledge | 226           | Being resilient | resume_attribute_00032_13 | 10
56      | 226 | Skills & Knowledge - Skills & Knowledge | 226           | Being resilient | resume_attribute_00032_13 | 11
56      | 226 | Skills & Knowledge - Skills & Knowledge | 226           | Being resilient | resume_attribute_00032_13 | 12
56      | 226 | Skills & Knowledge - Skills & Knowledge | 226           | Being resilient | resume_attribute_00032_13 | 13
56      | 226 | Skills & Knowledge - Skills & Knowledge | 226           | Being resilient | resume_attribute_00032_13 | 14
56      | 226 | Skills & Knowledge - Skills & Knowledge | 226           | Being resilient | resume_attribute_00032_13 | 15
56      | 226 | Skills & Knowledge - Skills & Knowledge | 226           | Being resilient | resume_attribute_00032_13 | 16
56      | 226 | Skills & Knowledge - Skills & Knowledge | 226           | Being resilient | resume_attribute_00032_13 | 17
56      | 226 | Skills & Knowledge - Skills & Knowledge | 226           | Being resilient | resume_attribute_00032_13 | 18

但我得到了这个:

            df             | user_id |            val            |         col_name          
---------------------------+---------+---------------------------+---------------------------
resume_attribute_00064_13 | 525     | resume_attribute_00064_13 | resume_attribute_00064_13
resume_attribute_00064_13 | 0       | resume_attribute_00064_13 | resume_attribute_00064_13
resume_attribute_00064_13 | 542     | resume_attribute_00064_13 | resume_attribute_00064_13
resume_attribute_00064_13 | 326     | resume_attribute_00064_13 | resume_attribute_00064_13
resume_attribute_00064_13 | 564     | resume_attribute_00064_13 | resume_attribute_00064_13
resume_attribute_00064_13 | 86      | resume_attribute_00064_13 | resume_attribute_00064_13
resume_attribute_00064_13 | 162     | resume_attribute_00064_13 | resume_attribute_00064_13
resume_attribute_00064_13 | 603     | resume_attribute_00064_13 | resume_attribute_00064_13
resume_attribute_00064_13 | 803     | resume_attribute_00064_13 | resume_attribute_00064_13
resume_attribute_00064_13 | 246     | resume_attribute_00064_13 | resume_attribute_00064_13

对此有何帮助?

错误/查询样本无效:

select * from (
  select
    display_field as df,
    subq.*
  from company_resumesectionitem rsi, lateral (
      select
          user_id,
          unnest(string_to_array(format(rsi.display_field), ',')) as val,
          format(rsi.display_field::text) as col_name
          from import.vw_rpt_resume) subq
) as sel;

0 个答案:

没有答案