Search code examples
sqlgoogle-bigqueryunpivot

SQL BigQuery Rows to column


This is a sample of my data

enter image description here

I am trying to get :

enter image description here

Thank you


Solution

  • You can unpivot in BigQuery using unnest() and arrays:

    select t.holiday, t.segment, el.dte, el.booking
    from t cross join
         unnest(array[struct('1/1/2020' as dte, "1/1/2020" as booking),
                      struct('1/2/2020' as dte, "1/2/2020" as booking),
                      . . .
                     ]
                ) el;