Skip to content
Advertisement

How to unnest multilevel structs in BigQuery?

I have many tables with different table suffixes in BigQuery where each table has four level structs in each row. I made query to unnest it into flattening table.

SELECT
  report_date,
  tracker,
  source,
  countryCode,
  stageNum,
  bundleId,
  offer_name,
  places.name AS site_id,
  places.clicks,
  places.cost,
  places.p_leads_v2_
FROM (
  SELECT
    report_date,
    tracker,
    source,
    countryCode,
    stageNum,
    bundleId,
    offer.name AS offer_name,
    offer.places
  FROM (
    SELECT
      report_date,
      tracker,
      source,
      campaigns.countryCode,
      campaigns.stageNum,
      campaigns.bundleId,
      campaigns.offer
    FROM
      `dv3.tracker_recording.records_*` AS t,
      t.campaigns ) AS t1,
    t1.offer) AS t2,
  t2.places

It works correct but not very well because it is seems like too long. How can I make it simpler and more effective? Any suggestions. Thank you.

Advertisement

Answer

Did you try query similar to this?

SELECT 
report_date,
tracker,
source,
c.countryCode,
c.stageNum,
c.bundleId,   
o.name as offer_name,
p.name AS site_id,
p.clicks,
p.cost,
p.p_leads_v2_
FROM `dv3.tracker_recording.records_*` as t, UNNEST(t.campaigns) as c, UNNEST(c.offer) as o, UNNEST(o.places) as p
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement