clickhouse查询语法--array join

array join

array join子句允许在数据表的内部,与数组或嵌套类型的字段进行join操作,从而将一行数组展开为多行,类似于hive中的exploded炸裂函数的功能

create table tb_array_join(id Int8,hobby Array(String))engine=Log;
insert into tb_array_join values
(1,['eat','drink','sleep']),
(2,array('study','sport','read'));

select * from tb_array_join;
┌─id─┬─hobby────────────────────┐
│  1['eat','drink','sleep']  │
│  2['study','sport','read'] │
└────┴──────────────────────────┘

select h from tb_array_join array join hobby as h;
┌─h─────┐
│ eat   │
│ drink │
│ sleep │
│ study │
│ sport │
│ read  │
└───────┘

select id,hobby,arrayEnumerate(hobby) as indexs from tb_array_join;
┌─id─┬─hobby────────────────────┬─indexs──┐
│  1['eat','drink','sleep'][1,2,3] │
│  2['study','sport','read'][1,2,3] │
└────┴──────────────────────────┴─────────┘

insert into tb_array_join values(3,['run','jump']);
select id,hobby,arrayEnumerate(hobby) as indexs from tb_array_join;
┌─id─┬─hobby────────────────────┬─indexs──┐
│  1['eat','drink','sleep'][1,2,3] │
│  2['study','sport','read'][1,2,3] │
└────┴──────────────────────────┴─────────┘
┌─id─┬─hobby──────────┬─indexs─┐
│  3['run','jump'][1,2]  │
└────┴────────────────┴────────┘

或者
with arrayEnumerate(hobby) as index 
select id,hobby,index from tb_array_join; 
┌─id─┬─hobby──────────┬─index─┐
│  3['run','jump'][1,2] │
└────┴────────────────┴───────┘
┌─id─┬─hobby────────────────────┬─index───┐
│  1['eat','drink','sleep'][1,2,3] │
│  2['study','sport','read'][1,2,3] │
└────┴──────────────────────────┴─────────┘

select id,hobby,arrayEnumerate(hobby) as idx,h,xx 
from tb_array_join array join hobby as h,idx as xx;
┌─id─┬─hobby────────────────────┬─idx─────┬─h─────┬─xx─┐
│  1['eat','drink','sleep'][1,2,3] │ eat   │  1 │
│  1['eat','drink','sleep'][1,2,3] │ drink │  2 │
│  1['eat','drink','sleep'][1,2,3] │ sleep │  3 │
│  2['study','sport','read'][1,2,3] │ study │  1 │
│  2['study','sport','read'][1,2,3] │ sport │  2 │
│  2['study','sport','read'][1,2,3]read3 │
└────┴──────────────────────────┴─────────┴───────┴────┘
┌─id─┬─hobby──────────┬─idx───┬─h────┬─xx─┐
│  3['run','jump'][1,2] │ run  │  1 │
│  3['run','jump'][1,2] │ jump │  2 │
└────┴────────────────┴───────┴──────┴────┘

简化一下
select id,h,xx from tb_array_join array join hobby as h,arrayEnumerate(hobby) as xx;
┌─id─┬─h─────┬─xx─┐
│  1 │ eat   │  1 │
│  1 │ drink │  2 │
│  1 │ sleep │  3 │
│  2 │ study │  1 │
│  2 │ sport │  2 │
│  2read3 │
└────┴───────┴────┘
┌─id─┬─h────┬─xx─┐
│  3 │ run  │  1 │
│  3 │ jump │  2 │
└────┴──────┴────┘
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值