【问题标题】:Split array by custom slice按自定义切片拆分数组
【发布时间】:2022-01-07 04:14:38
【问题描述】:

我有一个数组

[('active',1),('active',2),('active',3),('active',4), ('not active',7),('not active',5),('not active',6),('active',7),('active',8),('active',9)]

如果有办法得到这样的数组:

[[('active',1),('active',2),('active',3), ('active',4)],[('not active',7),('not active',5),('not active',6)],[('active',7),('active',8),('active',9)]]

【问题讨论】:

    标签: clickhouse


    【解决方案1】:
    SELECT arraySplit((z, w) -> w, x, arrayMap((i, j) -> ((i.1) != (j.1)), x, arrayPushFront(arrayPopBack(x), ('-', 0)))) AS y
    FROM
    (
        SELECT [('active', 1), ('active', 2), ('active', 3), ('active', 4), ('not active', 7), ('not active', 5), ('not active', 6), ('active', 7), ('active', 8), ('active', 9)] AS x
    )
    
    ┌─y─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
    │ [[('active',1),('active',2),('active',3),('active',4)],[('not active',7),('not active',5),('not active',6)],[('active',7),('active',8),('active',9)]] │
    └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
    

    【讨论】:

      【解决方案2】:
      with e as (
          select '1' sku, 'active' status, toInt32('11') dttm
          union all
          select '1' sku, 'active' status, toInt32('12') dttm
          union all
          select '1' sku, 'active' status, toInt32('13') dttm
          union all
          select '1' sku, 'not active' status, toInt32('14') dttm
          union all
          select '1' sku, 'not active' status, toInt32('15') dttm
          union all
          select '1' sku, 'not active' status, toInt32('16') dttm
          union all
          select '1' sku, 'active' status, toInt32('17') dttm
          union all
          select '1' sku, 'active' status, toInt32('18') dttm
          union all
          select '1' sku, 'active' status, toInt32('19') dttm
      )
      , 'active' as ch
      select
          sequence
          , arrayEnumerate(sequence) indexes
          , arraySplit((x, i) -> (sequence.1)[i] = ch and (sequence.1)[i-1] <> ch or (sequence.1)[i] <> ch and (sequence.1)[i - 1] = ch, sequence, indexes) res
      from (
          SELECT groupArray(tuple(status, dttm)) as sequence
          from e
          group by sku
      ) a
      

      输出

      +-------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
      |sequence                                                                                                                                   |res                                                                                                                                              |
      +-------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
      |[('active',11),('active',12),('active',13),('not active',14),('not active',15),('not active',16),('active',17),('active',18),('active',19)]|[[('active',11),('active',12),('active',13)],[('not active',14),('not active',15),('not active',16)],[('active',17),('active',18),('active',19)]]|
      +-------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      • 2015-09-02
      • 2021-05-03
      • 2016-04-16
      • 2021-04-27
      • 2012-11-10
      相关资源
      最近更新 更多