【问题标题】:Clickhouse - Group by within nested columnsClickhouse - 在嵌套列中分组
【发布时间】:2019-05-27 10:56:54
【问题描述】:

如何在嵌套列中执行 group by

我有一个嵌套列 items.productNameitems.amount

我想获得按 productName 的每个值分组的金额总和。

我可以使用 array join

来实现这一点
    SELECT items.productName as name, sum(items.amount) as amt from test 
    array join items
    group by items.productName

但是数组连接很慢,所以我们不能使用它们。

所以我尝试使用 sumForEach() 但我不确定如何按单个产品名称对结果进行分组

    SELECT items.productName as name, sumForEach(items.amount) as amt from test
    group by name

我可以不使用数组连接来实现这个功能吗?

谢谢。

【问题讨论】:

    标签: grouping clickhouse


    【解决方案1】:

    作为替代可以使用sumMap-function:

    select result.1 as name, result.2 as amt
    from (
          select sumMap(items.productName, items.amount) sum_per_keys,
                arrayJoin(arrayZip(sum_per_keys.1, sum_per_keys.2)) result
          from nested_columns_test)
    order by name;
    

    对 CH 20.3.8.53 的测试表明 sumMaparray join 慢得多


    准备测试环境:

    create table nested_columns_test(
      id Int32,
      items Nested(productName String, amount Int32)
    ) Engine = MergeTree()
    order by (id);
    
    insert into nested_columns_test
    select number as id,
          arrayMap(x -> concat('product_', toString(x)), range(number % 32)) as `items.productName`, 
          arrayMap(x -> number + x, range(number % 32)) as `items.amount` 
    from numbers(100*1000*1000);
    
    SELECT items.productName as name, sum(items.amount) as amt 
    from nested_columns_test 
    array join items
    group by items.productName
    order by name;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 2020-03-29
      • 2021-04-04
      • 1970-01-01
      • 2017-04-02
      • 2015-09-19
      相关资源
      最近更新 更多