如何在不指定列中所有值的情况下转置 BigQuery 中的单个列?

How to transpose a single column in BigQuery without specifying all the values in the column?

我有一个 table 类型如下:

col_A col_B col_C col_D
 1     ab    cd    val1
 2     cd    ef    val2
 2     cd    ef    val3
 3     ab    bc    val4

我想做的是以下事情:

col_A col_B col_C val1 val2 val3 val4
 1     ab    cd    1    0    0    0
 2     cd    ef    0    1    1    0
 3     ab    bc    0    0    0    1

基本上转置 col_4 以便其中的每个条目都有一列,值 1 表示行中存在该值,值 0 表示它不存在在行中。

挑战在于我不知道 col_4 中可以包含的所有值,因此我需要能够创建这样的视图,而无需创建特定于 [=20= 中某些值的 case 语句] 创建新列。知道怎么做吗?

希望这对您有所帮助:您说过您事先不知道 col_D 中的值,因此需要使用 EXECUTE IMMEDIATE.[=16= 动态创建数据透视查询]

CREATE TEMP TABLE sample AS
SELECT * FROM UNNEST([STRUCT
  (1 AS col_A, 'ab' AS col_B, 'cd' AS col_C, 'val1' AS col_D),
  (2, 'cd', 'ef', 'val2'),
  (2, 'cd', 'ef', 'val3'),
  (3, 'ab', 'bc', 'val4')
]);

EXECUTE IMMEDIATE FORMAT("""
  SELECT * FROM sample PIVOT (COUNT(col_A) FOR col_D IN ('%s'));
""", ARRAY_TO_STRING(ARRAY(SELECT DISTINCT col_D FROM sample ORDER BY 1), "','"));

输出:

如果您希望在您的问题中得到完全相同的结果,请尝试这个而不是之前的查询。

EXECUTE IMMEDIATE FORMAT("""
  SELECT * 
    FROM (SELECT col_A AS col, * FROM sample)
   PIVOT (COUNT(col) FOR col_D IN ('%s')) 
   ORDER BY 1;
""", ARRAY_TO_STRING(ARRAY(SELECT DISTINCT col_D FROM sample ORDER BY 1), "','"));

或者只是简单地

EXECUTE IMMEDIATE FORMAT("""
  SELECT * FROM sample PIVOT (COUNT(*) FOR col_D IN ('%s'));
""", ARRAY_TO_STRING(ARRAY(SELECT DISTINCT col_D FROM sample ORDER BY 1), "','"));         

有输出