【问题标题】:BigQuery ARRAY_AGG(STRUCT) splitting values basing on column valueBigQuery ARRAY_AGG(STRUCT) 根据列值拆分值
【发布时间】:2019-05-29 20:25:53
【问题描述】:

我有一个这样的 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

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

我不知道是否可以根据 category 值的 DISTINCT 值进行动态列定义,但在更简单的情况下,我可以使用固定值 cat1cat2

这是我描述的输出示例:

+------+------------+--------------------+---------------------+--------------------+---------------------+
| Name |    Date    | cat1_grouped.value | cat1_grouped.number | cat2_grouped.value | cat2_grouped.number |
+------+------------+--------------------+---------------------+--------------------+---------------------+
| John | 2019-01-03 | AA                 |                  10 | NN                 |                  12 |
|      |            | AB                 |                  11 | MM                 |                  13 |
|      |            |                    |                     |                    |                     |
+------+------------+--------------------+---------------------+--------------------+---------------------+

【问题讨论】:

    标签: google-bigquery


    【解决方案1】:

    以下是工作示例 - 适用于 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   
    

    【讨论】:

    • 我不知道ARRAY_CONCAT_AGG。谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    相关资源
    最近更新 更多