在BigQuery中加入阵列栏位

时间:2018-09-20 17:57:18

标签: google-bigquery

We have tables Table1 and Table2

Table 1

ID name  subject      
1  xxx   physics   
         maths
         chemistry
2  yyy   physics   
         maths
         chemistry
Table 2

Id   Marks
1    70
     67
     88
2    90
     99
     89

We need to join the above two tables like this format with condition Table1.Id=Table2.Id,

Id    name   subject       Marks
1     xxx    physics       70
             maths         67
             chemistry     88
2     yyy    physics       90
             maths         99
             chemistry     89

1 个答案:

答案 0 :(得分:1)

   
#standardSQL
WITH `project.dataset.table1` AS (
  SELECT 1 id, 'xxx' name, ['physics', 'maths', 'chemistry'] subject UNION ALL
  SELECT 2, 'yyy', ['physics', 'maths', 'chemistry'] 
), `project.dataset.table2` AS (
  SELECT 1 id, [70, 67, 88] marks UNION ALL
  SELECT 2, [90, 99, 89] 
)
SELECT *
FROM `project.dataset.table1` t1
JOIN `project.dataset.table2` t2
USING(id)

有结果

Row id  name    subject     marks    
1   1   xxx     physics     70   
                maths       67   
                chemistry   88   
2   2   yyy     physics     90   
                maths       99   
                chemistry   89