gpt4 book ai didi

hadoop - hive 列到行

转载 作者:可可西里 更新时间:2023-11-01 15:52:43 26 4
gpt4 key购买 nike

如何根据存储在现有列中的类型在 HIVE 中创建新列?

例如,我有:

id    ProductType
1 car
2 bike
3 truck

我想拥有:

id    car    bike    truck
1 1 null null
2 null 1 null
3 null null 1

最佳答案

Use `case` statement to convert values into columns:

select id, case when ProductType = 'car' then 1 end as car,
case when ProductType = 'bike' then 1 end as bike,
case when ProductType = 'truck' then 1 end as truck
from your_table;

OK
id car bike truck
1 1 NULL NULL
2 NULL 1 NULL
3 NULL NULL 1
Time taken: 35.442 seconds, Fetched: 3 row(s)

关于hadoop - hive 列到行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47267965/

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