Skip to content

Instantly share code, notes, and snippets.

@Osiyuks
Last active June 23, 2020 14:36
Show Gist options
  • Select an option

  • Save Osiyuks/d22af688335b99cc51f7972e010d4fdb to your computer and use it in GitHub Desktop.

Select an option

Save Osiyuks/d22af688335b99cc51f7972e010d4fdb to your computer and use it in GitHub Desktop.
with t as (
select 1 id, 'name=Alex; age=30; project=project1; company = MP' Data
union all
select 2, 'name=Vin; age=31; project=project3; company = AC'
union all
select 3, 'name=Sara; age=32; company = FF; hobby= cricket'
), g as (
Select id, split(flatdata,'=')[ORDINAL(1)] Key, split(flatdata,'=')[ORDINAL(2)] Value
from
(select id,split(Data,'; ') KeyValue from t )
left join unnest(KeyValue) flatdata
)
select id
,MAX(IF(Key = 'name', Value, NULL)) as name
,MAX(IF(Key = 'age', Value, NULL)) as age
,MAX(IF(Key = 'project', Value, NULL)) as project
,MAX(IF(Key = 'company ', Value, NULL)) as company
,MAX(IF(Key = 'hobby', Value, NULL)) as hobby
from g
group by 1 order by 1
@Zachm2
Copy link

Zachm2 commented Jun 23, 2020

with t as (

select 1 id, 'name=Alex; age=30; project=project1; company = MP' Data
union all
select 2, 'name=Vin; age=31; project=project3; company = AC'
union all
select 3, 'name=Sara; age=32; company = FF; hobby= cricket'

), g as (

Select id, split(flatdata,'=')[ORDINAL(1)] Key, split(flatdata,'=')[ORDINAL(2)] Value
from
(select id,split(Data,'; ') KeyValue from t )
left join unnest(KeyValue) flatdata

)

select id
,MAX(IF(Key = 'name', Value, NULL)) as name
,MAX(IF(Key = 'age', Value, NULL)) as age
,MAX(IF(Key = 'project', Value, NULL)) as project
,MAX(IF(Key = 'company ', Value, NULL)) as company
,MAX(IF(Key = 'hobby', Value, NULL)) as hobby
from g
group by 1 order by 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment