基于列的配置单元sql包数组

cu6pst1q  于 2021-06-26  发布在  Hive
关注(0)|答案(2)|浏览(294)

下面列出了多个列:

state          sport            size             color        name
florida        football         1                red          Max
nevada         football         1                red          Max
ohio           football         1                red          Max
texas          football         1                red          Max
florida        hockey           1                red          Max
nevada         hockey           1                red          Max
ohio           hockey           1                red          Max
texas          hockey           1                red          Max
florida        tennis           2                green        Max
nevada         tennis           2                green        Max
ohio           tennis           2                green        Max
texas          tennis           2                green        Max

有没有一种方法可以基于一个列(在本例中是名称)将这些内容组合到数组中,就像下面所需的输出那样。mac结果将有一条记录,而不是重复记录,记录将包含在一个数组中。

state                                   sport
[florida, nevada, ohio,texas]           [football, hockey, tennis]
size                                    color
[1,2]                                   [red, green]
velaa5lx

velaa5lx1#

你可以用 collect_set .

select name,collect_set(state),collect_set(sport),collect_set(size),collect_set(color)
from tbl
group by name
soat7uwm

soat7uwm2#

你需要使用集合。希望这有帮助。谢谢。

query:
select collect_set(state), 
collect_set(sport), 
collect_set(size), 
collect_set(color)
from myTable
where name = 'Max';

相关问题