QuickSight: Split string field by delimiter in custom SQL

0

Hi, i have a table in Athena which has 'testcase' and 'tags' fields. here is the sample records of the table:

  • testcase1 has tags 'tag1,tag2,tag3'
  • testcase2 has tags 'tag2,tag4'
  • testcase3 has tags 'tag2'
  • testcase4 does not has tags

I want to get all the tags from this table in QuickSight

  • tag1
  • tag2
  • tag3
  • tag4

i've tried with adding new dataset from Athena and choose Use Custom SQL option, using STRING_SPLIT function, but cannot make it work. Can you please help me with the sql query? Many Thanks, Yj Liu

질문됨 일 년 전1024회 조회
1개 답변
1
수락된 답변

Hi,

the question is how do you want to receive the data for tags?

in different columns or in different rows?

If it is ok to have it in multiple rows the following query using the function SPLIT to create an ARRAY and the unnest the results in the array on multiple rows, as described here, could help:

 with test_data as (select 
    'testcase1' as testcase, 'tag1,tag2,tag3' as tags
union all
select 
    'testcase2'as testcase , 'tag2,tag4'  as tags
union all
select 
    'testcase3' as testcase, 'tag2'  as tags
union all
select 
    'testcase4' as testcase,''  as tags ) 
select testcase, tag  from test_data
cross join unnest(SPLIT(tags, ',')) as t(tag)

result set is:

Enter image description here

If you want it in multiple columns you should use the SPLIT_PART function as shown here:

with test_data as (select 
    'testcase1' as testcase, 'tag1,tag2,tag3' as tags
union all
select 
    'testcase2'as testcase , 'tag2,tag4'  as tags
union all
select 
    'testcase3' as testcase, 'tag2'  as tags
union all
select 
    'testcase4' as testcase,''  as tags ) 
select testcase, SPLIT_PART(tags, ',',1) as tag1, SPLIT_PART(tags, ',',2) as tag2,
    SPLIT_PART(tags, ',',3) as tag3, SPLIT_PART(tags, ',',4) as tag4
    from test_data

result set is:

Enter image description here

hope this helps

AWS
전문가
답변함 일 년 전
  • Hi Fabrizio, Thank you so much for the reply! to have the tags in multi rows is what i'm looking for! save both in my notebook!

    Thank you again! Yj Liu

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠