PgSql 行转列的使用 jsonb_each与row_to_json
PgSql 行转列的使用 jsonb_each与row_to_json
1:正常的几行数据
2:转换后
3:code (以commodity来分组)
select "Id" , "JabilPn" ,"Brand" ,"PricelnUsd" ,"Commodity" from "Eme_Materials" em where "Id" <=3 select "Commodity",(q.qq).key as "key",(q.qq).value as "value" from ( with a as ( select "Id" , "JabilPn" ,"Brand" ,"PricelnUsd" ,"Commodity" from "Eme_Materials" em where "Id" <=3 ) select "Commodity" ,jsonb_each(row_to_json(a)::jsonb-'Commodity'::varchar) as qq from a )q
如有疑问或者错误的地方,请跟帖,本人会第一时间答复以及相互学习,谢谢!个人会不断的上传自己的学习心得!
好了今天就先到这里,下次有时间再更新,如果存在不合理的地方,欢迎大家多多指教留言!!!