converting array of strings to single row values

◇◆丶佛笑我妖孽 提交于 2020-06-13 02:09:03

问题


I have records in Big Query table as
name value
Aashis ["AB",AC"]
Rahul ["AA",AD"]

Here name and value column is String Type

I want the output as
name value
Aashis AB
Aashis AC
Rahul AA
Rahul AD


回答1:


Below is for BigQuery Standard SQL

#standardSQL
SELECT name, value
FROM `project.dataset.table`,
UNNEST(value) value

You can test, play with above using dummy data from your question as below

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 'Aashis' name, ["AB","AC"] value UNION ALL
  SELECT 'Rahul', ["AA","AD"]
)
SELECT name, value
FROM `project.dataset.table`,
UNNEST(value) value

with result

Row name    value    
1   Aashis  AB   
2   Aashis  AC   
3   Rahul   AA   
4   Rahul   AD     

Update for: the column value is of Type String and Unnest accepts array.How do I convert this column to array ?

See below example

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 'Aashis' name, '["AB","AC"]' value UNION ALL
  SELECT 'Rahul', '["AA","AD"]'
)
SELECT name, value
FROM `project.dataset.table`,
UNNEST(SPLIT(REGEXP_REPLACE(value, r'^\[|]$', ''))) value   

with result

Row name    value    
1   Aashis  "AB"     
2   Aashis  "AC"     
3   Rahul   "AA"     
4   Rahul   "AD"     


来源:https://stackoverflow.com/questions/52628342/converting-array-of-strings-to-single-row-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!