Bigquery - 将数组转置为列

Big Query - Transpose arrays into colums

我们在 Big Query 中有一个 table,如下所示。

输入table:

 Name | Interests
 -----+----------
 Bob  | ["a"]
 Sue  | ["a","b"]
 Joe  | ["b","c"]

我们想将上面的 table 转换为下面的格式以使其 BI/Visualisation 友好。

Target/Required table:

 +------------------+
 | Name | a | b | c |
 +------------------+
 | Bob  | 1 | 0 | 0 |
 | Sue  | 1 | 1 | 0 |
 | Joe  | 0 | 1 | 0 |
 +------------------+

注意:兴趣列是数组数据类型。这种转换在 Big Query 中是否可行?如果是,有参考查询吗?

提前致谢!

以下适用于 BigQuery Standard SQL 并使用 BQ

的脚本功能
#standardSQL
create temp table ttt as (
  select name, interest 
  from `project.dataset.table`, 
  unnest(interests) interest
);

EXECUTE IMMEDIATE (
  SELECT """
  SELECT name, """ || 
    STRING_AGG("""MAX(IF(interest = '""" || interest || """', 1, 0)) AS """ || interest, ', ') 
  || """
  FROM ttt 
  GROUP BY name
  """
  FROM (
    SELECT DISTINCT interest 
    FROM ttt
    ORDER BY interest
  )
);      

如果应用于您问题中的示例数据 - 输出为