I have a BigQuery table with an array column, with multiple (1 to 4) key:value pairs separated by a pipe "|". I want to pull the key:value pairs and add additional columns with the 'key' as the column header and the 'value' as... well the value/entry.
However, while there are uniform "keys" they are not all placed in the same order, therefore splitting by order doesn't quiet work. I've looked around and explored "JSON_EXTRACT_SCALAR" and "UNNEST" (from this question/answer: fetch key value pairs from array objects in sql BigQuery) but can't get desired outcome. I've also explored using "OFFSET" but don't know how to put it all together.
Here's what data field (they array) looks like:
Row campaignLabels
1 Segment: Rivers Non-Brand | Strategy: All Else | Category: Non-Brand | CN:Pause_5-29-19
2 Segment: Rivers Non-Brand | Category: Non-Brand | Strategy: All Else | CN:Pause_5-29-19
3 Category: Upper Funnel | Strategy: All Else
4 Strategy: All Else | Segment: Rivers Brand | Category: Brand
5 Strategy: All Else | Category: Brand | Segment: Rivers Brand
6 Segment: Rivers Non-Brand | Category: Non-Brand | Strategy: All Else
7 Strategy: All Else | Segment: Viking Other Brand | Category: Brand
8 Strategy: All Else | Category: Brand | Segment: Rivers Brand
9 Strategy: All Else | Category: Brand | Segment: Rivers Brand
10 Strategy: All Else | Category: Brand | Segment: Viking Other Brand
Ideally output is to query this same table pulling certain columns AND add columns with "Strategy", "Category", and "Segment" as column labels and the values as the value returned.
Help!
Some attempts that give me partially there but not the needed result:
SELECT
DISTINCT(SUBSTR(Part1, 10)) AS Strategy
FROM (
SELECT
Labels[OFFSET(0)] AS Part1,
Labels[OFFSET(1)] AS Part2,
Labels[SAFE_OFFSET(2)] AS Part3,
Labels[SAFE_OFFSET(3)] AS Part4
FROM (
SELECT
SPLIT(campaignLabels,"| ") AS Labels
FROM
`table_A` )
)
WHERE Part1 LIKE "Strategy:%"
Below is for BigQuery Standard SQL
#standardSQL
select campaignLabels,
( select as struct
max(if(key = 'Segment', value, null)) as Segment,
max(if(key = 'Strategy', value, null)) as Strategy,
max(if(key = 'Category', value, null)) as Category
from (
select as struct kv[offset(0)] as key, trim(kv[offset(1)]) as value
from t.labels label,
unnest([struct(split(label, ':') as kv)])
)
).*
from `project.dataset.table`,
unnest([struct(split(campaignLabels, ' | ') as labels)]) t
if to apply to sample data from y our question - output is