Sql groupby并在配置单元中存储为字典字符串
所以,我有一张桌子Sql groupby并在配置单元中存储为字典字符串,sql,hive,Sql,Hive,所以,我有一张桌子 user_id, product, price 12, foobar, 1.2 12, foo, 2.2 13, baz, 2 14, biz, 20 14, buzz, 21 and so on.. 我想按用户id分组,基本上存储为以下模式 schema: user_id int, product_prices string 结果是 12\t foobar,1.2\tfoo,2.2 13\t baz,2 14\t biz,20\tb
user_id, product, price
12, foobar, 1.2
12, foo, 2.2
13, baz, 2
14, biz, 20
14, buzz, 21
and so on..
我想按用户id分组,基本上存储为以下模式
schema:
user_id int,
product_prices string
结果是
12\t foobar,1.2\tfoo,2.2
13\t baz,2
14\t biz,20\tbuzz,21
等等
如何在hive(sqlish)中实现这一点?可以使用CTE、CONCAT和CONCAT\u ws实现 试试下面的代码
with temp as
( select user_id, CONCAT(product , ',' , cast(price as String)) res from your_table)
SELECT user_id, concat_ws("\t",collect_set(res))
FROM temp
GROUP BY user_id;
步骤1:用“,”标注产品和价格
步骤2:将“\t”的concat_ws用于concat数组集。可以使用CTE、concat和concat_ws完成
select user_id
,concat_ws('\t',collect_list(concat_ws(',',product,cast(price as string))))
from mytable
group by user_id
;
试试下面的代码
with temp as
( select user_id, CONCAT(product , ',' , cast(price as String)) res from your_table)
SELECT user_id, concat_ws("\t",collect_set(res))
FROM temp
GROUP BY user_id;
步骤1:用“,”标注产品和价格
步骤2:将“\t”的concat_ws用于concat数组集
select user_id
,concat_ws('\t',collect_list(concat_ws(',',product,cast(price as string))))
from mytable
group by user_id
;
。。。如果字典(地图)真的是你想要的:
select user_id
,str_to_map(concat_ws(',',collect_list(concat_ws(':',product,cast(price as string)))))
from mytable
group by user_id
;
。。。如果字典(地图)真的是你想要的:
select user_id
,str_to_map(concat_ws(',',collect_list(concat_ws(':',product,cast(price as string)))))
from mytable
group by user_id
;
请求的格式不清楚。您对结果做了什么?请求的格式不清楚。你对结果做了什么?