基于列值的BigQuery ARRAY_AGG(STRUCT)拆分值

时间:2019-01-03 08:57:56

标签: google-bigquery

我有一个这样的BigQuery表:

+------+------------+----------+-------+--------+
| Name |    Date    | Category | Value | Number |
+------+------------+----------+-------+--------+
| John | 2019-01-03 | Cat1     | AA    |     10 |
| John | 2019-01-03 | Cat1     | AB    |     11 |
| John | 2019-01-03 | Cat2     | NN    |     12 |
| John | 2019-01-03 | Cat2     | MM    |     13 |
+------+------------+----------+-------+--------+

前2列是键标识符,我需要根据这2列对行进行阵列/分组。

这是示例语句:

WITH data AS (
  SELECT "John" name, DATE("2019-01-03") date, "cat1" category, "AA" value, 10 number
  UNION ALL
  SELECT "John", DATE("2019-01-03"), "cat1", "AB", 11
  UNION ALL
  SELECT "John", DATE("2019-01-03"), "cat2", "NN", 12
  UNION ALL
  SELECT "John", DATE("2019-01-03"), "cat2", "MM", 13
)

SELECT * FROM data

查询的基本版本非常简单:

SELECT 
  name,
  date,
  ARRAY_AGG(
    STRUCT<category STRING, value STRING, number INT64>(category,value,number)
  ) AS items

FROM data
GROUP BY 1,2

但是在我的情况下,我需要根据value列区分number-category分组值(在2个不同的列上)

我不知道是否可以基于category值的DISTINCT值来创建动态列定义,但是在更简单的情况下,我可以使用固定值cat1和{{1 }}

这是我描述的输出示例:

cat2

1 个答案:

答案 0 :(得分:2)

以下是工作示例-适用于BigQuery标准SQL

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 'John' name, DATE '2019-01-03' dt, 'Cat1' category, 'AA' value, 10 number UNION ALL
  SELECT 'John', '2019-01-03', 'Cat1', 'AB', 11 UNION ALL
  SELECT 'John', '2019-01-03', 'Cat2', 'NN', 12 UNION ALL
  SELECT 'John', '2019-01-03', 'Cat2', 'MM', 13 
)
SELECT name, dt,
  ARRAY_CONCAT_AGG(IF(category = 'Cat1', arr, [])) cat1_grouped,
  ARRAY_CONCAT_AGG(IF(category = 'Cat2', arr, [])) cat2_grouped
FROM (
  SELECT name, dt, category,
    ARRAY_AGG(STRUCT<value STRING, number INT64>(value, number)) arr
  FROM `project.dataset.table`
  GROUP BY name, dt, category
)
GROUP BY name, dt

有结果

Row name    dt          cat1_grouped.value  cat1_grouped.number cat2_grouped.value  cat2_grouped.number  
1   John    2019-01-03  AA                  10                  NN                  12    
                        AB                  11                  MM                  13