gpt4 book ai didi

sql - 如何根据某些条件在配置单元中获取收集集

转载 作者:可可西里 更新时间:2023-11-01 16:37:33 25 4
gpt4 key购买 nike

如何根据某些条件获取hive中的collect set

id | num_of_cats
=====================
HOP A

HOP B

HOP C

CAP A

CAP C

CAP B

TOP C

如果指标是 A 则第一个字段是 1 。顺序是A,B,C例如:第一行仅包含 A,因此指标为 1,0,0第二行只包含 B 所以指标是 0,1,0

应该返回:

id | cats_aggregate    (indicator order is A,B,C)
===========================
HOP Array<int>(1,0,0)

HOP Array<int>(0,1,0)

HOP Array<int>(0,0,1)

CAP Array<int>(1,0,0)

CAP Array<int>(0,0,1)

CAP Array<int>(0,1,0)

TOP Array<int>(0,0,1)

最佳答案

这应该做你想做的:

select id,
array( (case when num_of_cats = 'A' then 1 else 0 end),
(case when num_of_cats = 'B' then 1 else 0 end),
(case when num_of_cats = 'C' then 1 else 0 end)
)
from t;

关于sql - 如何根据某些条件在配置单元中获取收集集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48907392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com