QuickSight:在自定义SQL中通过分隔符拆分字符串字段

0

【以下的问题经过翻译处理】 你好, 我在Athena中有一张数据表,其中包含“testcase”和“tags”字段。以下是表的样例记录:

  • testcase1拥有标签“tag1,tag2,tag3”
  • testcase2拥有标签“tag2,tag4”
  • testcase3拥有标签“tag2”
  • testcase4没有标签

我想在QuickSight中获取这个表的所有标签:

  • tag1
  • tag2
  • tag3
  • tag4

我尝试添加来自Athena的新数据集,并选择“使用自定义SQL”选项,使用STRING_SPLIT函数,但无法使其工作。请问你能帮我写SQL查询语句吗? 非常感谢, Yj Liu

profile picture
專家
已提問 5 個月前檢視次數 39 次
1 個回答
0

【以下的回答经过翻译处理】 你好,

问题是你想如何接收标签数据?

是将它们放在不同的行或不同的列中吗?

如果可以将它们放在多行中,可以使用以下查询语句,使用SPLIT函数创建一个数组,然后使用该数组的结果在多行中使用unnest方法进行展开,详见这里:

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)

结果为:

Enter image description here

如果你想要它在多列中,你应该使用SPLIT_PART函数,如下所示:

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

结果为:

Enter image description here

希望能帮到你

profile picture
專家
已回答 5 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南