【问题标题】:Array element count in clickhouseclickhouse 中的数组元素计数
【发布时间】:2021-10-29 03:01:14
【问题描述】:

我有一张这样的桌子:

| key  | seq   |    words    |
|  '1' | '123' |['AAA', 'BBB']|
|  '1' | '456' |['AAA', 'CCC']|

我想获取键为“1”的每个元素的计数,例如

| key |         result_words         |
| '1' |[('AAA',2),('BBB',1),('CCC',1)|

我尝试了一些数组函数但失败了。最好的方法是什么?

【问题讨论】:

    标签: clickhouse


    【解决方案1】:
    create table test Engine=Memory as 
    select  '1' key , '123' seq, ['AAA', 'BBB'] words union all
    select  '1', '456' ,['AAA', 'CCC'];
    
    SELECT
        key,
        arrayZip((sumMap(words, arrayResize(CAST([], 'Array(UInt64)'), length(words), 1)) AS x).1, x.2) AS r
    FROM test
    GROUP BY key
    
    ┌─key─┬─r───────────────────────────────┐
    │ 1   │ [('AAA',2),('BBB',1),('CCC',1)] │
    └─────┴─────────────────────────────────┘
    

    【讨论】:

      【解决方案2】:
      SELECT key, groupArray((word, count)) AS result
      FROM 
      (
          SELECT key, word, count() AS count
          FROM 
          (
              /* Emulate the test dataset. */
              SELECT data.1 AS key, data.2 AS seq, data.3 AS words
              FROM 
              (
                  SELECT arrayJoin([
                      ('1', '123', ['AAA', 'BBB']), 
                      ('1', '456', ['AAA', 'CCC'])]) AS data
              )
          )
          ARRAY JOIN words AS word
          GROUP BY key, word
      )
      GROUP BY key
      
      /*
      ┌─key─┬─result──────────────────────────┐
      │ 1   │ [('BBB',1),('AAA',2),('CCC',1)] │
      └─────┴─────────────────────────────────┘
      */
      

      【讨论】:

      • 我们可以在ARRAY JOIN之后,GROUP BY之前添加WHERE子句来按键过滤
      猜你喜欢
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      • 1970-01-01
      • 2020-09-17
      • 2023-01-19
      • 2023-02-24
      相关资源
      最近更新 更多