【问题标题】:how to use array join in Clickhouse如何在 Clickhouse 中使用数组连接
【发布时间】:2021-06-19 10:01:35
【问题描述】:

我正在尝试使用 arrayJoin() 拆分 2 个数组

我的桌子:

create table test_array(
    col1 Array(INT),
    col2 Array(INT),
    col3 String
)
engine = TinyLog;

然后我插入这些值:

insert into test_array values ([1,2],[11,22],'Text');
insert into test_array values ([5,6],[55,66],'Text');

当我在 col1 中拆分第一个数组时,结果将是这样的:

但我需要的是拆分 col1 和 col2 并将它们添加到选择中。

我试过这个查询,但没有用。

select arrayJoin(col1)  ,arrayJoin(col2) ,col1 , col2 ,col3 from test_array;

如何编辑查询以删除图片中的突出显示的行

谢谢。

【问题讨论】:

标签: clickhouse


【解决方案1】:

arrayJoin 的串行调用产生笛卡尔积,避免使用ARRAY JOIN

SELECT
    c1,
    c2,
    col1,
    col2,
    col3
FROM test_array
ARRAY JOIN
    col1 AS c1,
    col2 AS c2

/*
┌─c1─┬─c2─┬─col1──┬─col2────┬─col3─┐
│  1 │ 11 │ [1,2] │ [11,22] │ Text │
│  2 │ 22 │ [1,2] │ [11,22] │ Text │
│  5 │ 55 │ [5,6] │ [55,66] │ Text │
│  6 │ 66 │ [5,6] │ [55,66] │ Text │
└────┴────┴───────┴─────────┴──────┘
*/

【讨论】:

  • 谢谢@vladimir
  • @alim np,我很乐意提供帮助。
【解决方案2】:

另一种方式——tuple()

SELECT
    untuple(arrayJoin(arrayZip(col1, col2))),
    col3
FROM test_array

┌─_ut_1─┬─_ut_2─┬─col3─┐
│     1 │    11 │ Text │
│     2 │    22 │ Text │
│     5 │    55 │ Text │
│     6 │    66 │ Text │
└───────┴───────┴──────┘

【讨论】:

  • 感谢@Denny Crane
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-05
  • 2019-08-18
  • 2021-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多