Google bigquery BigQuery数组_AGG(STRUCT)基于列值拆分值

Google bigquery BigQuery数组_AGG(STRUCT)基于列值拆分值,google-bigquery,Google Bigquery,我有这样一个BigQuery表: +------+------------+----------+-------+--------+ | Name | Date | Category | Value | Number | +------+------------+----------+-------+--------+ | John | 2019-01-03 | Cat1 | AA | 10 | | John | 2019-01-03 | Cat1 |

我有这样一个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 |
+------+------------+----------+-------+--------+
前两列是关键标识符,我需要根据这两列对行进行数组/分组

以下是示例语句:

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

下面是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

我不知道
ARRAY\u CONCAT\u AGG
。谢谢
#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