BigQuery: flatten two repeated columns

前端 未结 2 2012
鱼传尺愫
鱼传尺愫 2021-01-14 20:13

I am currently working with a very simple table on BigQuery and each row has two repeated columns class_numbers [REPEATED INTEGER] and class_

相关标签:
2条回答
  • 2021-01-14 20:56

    Using standard SQL, you can use UNNEST(...) WITH OFFSET. For example,

    SELECT
      class_number,
      class_descriptions[OFFSET(off)] AS class_description
    FROM MyTable,
      UNNEST(class_numbers) AS class_number WITH OFFSET off;
    
    0 讨论(0)
  • 2021-01-14 21:10

    Below is version for BigQuery Legacy SQL

    SELECT 
      class_number, 
      class_description
    FROM (
        SELECT 
          class_number, 
          number_pos, 
          class_descriptions AS class_description, 
          POSITION(class_descriptions) AS description_pos 
        FROM FLATTEN((
            SELECT 
              class_numbers AS class_number, 
              POSITION(class_numbers) AS number_pos, 
              class_descriptions 
            FROM YourTable
        ), class_numbers)
    )
    WHERE number_pos = description_pos    
    
    0 讨论(0)
提交回复
热议问题