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

后端 未结 3 705
爱一瞬间的悲伤
爱一瞬间的悲伤 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:03

    @JonScott, @AlexYes and other pals who struggle with similar kinda situations.

    I found more better approach other than suggested by @AlexYes.

    What I did, I flatter category column which result individual records. Which I can further process.

    Query:

    select row_number() over(order by 1) as r1, 
            to_char(timestamptz 'epoch' + date_time * interval '1 second', 'yyyy-mm-dd') AS DAY,
            split_part(categories, ';', numbers.n) as catg,
            value
        from 
        join numbers
        on numbers.n <= regexp_count(category_string, ';') + 1 
    
    
    

    Explanation:

    Two functions are useful here: first, the split_part function, which takes a string, splits it on ';' delimiter, and returns the first, second, ... , nth value specified from the split string; second, regexp_count, which tells us how many times a particular pattern is found in our string.

    提交回复
    热议问题