How to get substring for filter and group by clause in AWS Redshift database

后端 未结 3 743
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-28 07:29

How to get substring from column which contains records for filter and group by clause in AWS Redshift database.

I have table with records like:

Table_Id         


        
3条回答
  •  梦如初夏
    2021-01-28 08:05

    To do this fully dynamically, you need to transpose or pivot values in "categories" column into separate rows. Unfortunately, a "fully dynamic" solution (without knowing the different values beforehand) is NOT possible using redshift.

    Your options are as follows:

    1. Use the method suggested by AlexYes in another answer. This is semi-dynamic and is probably your best option.

    2. Outside of Redshift, run some ETL code to perform the column -> multiple rows ETL.

    3. Create a hardcoded type solution, and perform the pivot something like this:

      select table_id,'ABC1' as category, case when concat(Categories,';') ilike '%ABC1;%' then value else 0 end as value from your_table union all select table_id,'ABC1-1' as category, case when concat(Categories,';')ilike '%ABC1-1;%' then value else 0 end as value from your_table union all

    etc

提交回复
热议问题