Bigquery - 使用标准SQL展平表

时间:2018-01-16 20:56:36

标签: google-bigquery

跟进我之前的问题链接 - Bigquery- Struct format

这是我在图像-1中的当前表格格式,我想获取产品B的详细信息,如图像2所示,但不可能,因为它是数组格式。 所以从技术上来说,我想要展平我的桌子,如图3所示。有什么方法可以做到吗。请建议。

Image-1

Image-2 Image-3

图像1表的示例查询

WITH yourTable AS (
  SELECT 1 id,["A","B","C","D"] Product, [2013,1625,1297,7634] num, [2013,1625,1297,7634] num_2 UNION ALL
  SELECT 2,["A","B","C","D","E"] Product, [1,2,3,4,5], [1,2,3,4,5] 
) 


    SELECT id,Product,
      Array (
        SELECT num * num_2
        FROM (SELECT pos, num FROM UNNEST(num) num WITH OFFSET pos) a
        JOIN (SELECT pos_2, num_2 FROM UNNEST(num_2) num_2 WITH OFFSET pos_2) b
        ON a.pos = b.pos_2
      ) mul
    FROM yourTable

我很想知道是否可以在"选择"陈述但不在"与"言。

修改查询以获取我正在寻找的结果集。 如果有任何简单的方法,请建议。

WITH yourTable AS (
  SELECT 1 id,["A","B","C","D"] Product, [2013,1625,1297,7634] num, [2013,1625,1297,7634] num_2 UNION ALL
  SELECT 2,["A","B","C","D","E"] Product, [1,2,3,4,5], [1,2,3,4,5] 
) 
select id,product,num,num_2,mul
from

(select id,product,num,num_2,mul,pos,pos_2,pos_3,pos_4 

from

(SELECT id,Product,num,num_2,
  Array (
    SELECT num * num_2
    FROM (SELECT pos, num FROM UNNEST(num) num WITH OFFSET pos) a
    JOIN (SELECT pos_2, num_2 FROM UNNEST(num_2) num_2 WITH OFFSET pos_2) b
    ON a.pos = b.pos_2
  ) mul
FROM yourTable


), unnest(product) as product WITH OFFSET pos,
unnest (num) as num WITH OFFSET pos_2,
unnest (num_2) as num_2 WITH OFFSET pos_3,
unnest (mul) as mul WITH OFFSET pos_4) 

where pos=pos_2 and 
pos=pos_3
and pos=pos_4

1 个答案:

答案 0 :(得分:3)

     
#standardSQL
WITH yourTable AS (
  SELECT 1 id,["A","B","C","D"] Product, [2013,1625,1297,7634] num, [2013,1625,1297,7634] num_2 UNION ALL
  SELECT 2,["A","B","C","D","E"] Product, [1,2,3,4,5], [1,2,3,4,5] 
) 
SELECT id, Product, num, num_2, num * num_2 mul
FROM yourTable, UNNEST(num) num WITH OFFSET pos
JOIN UNNEST(num_2) num_2 WITH OFFSET pos_2 ON pos = pos_2
JOIN UNNEST(Product) Product WITH OFFSET pos_3 ON pos = pos_3