简体   繁体   English

如何将列值转换为行 PostgreSQL

[英]How to convert column values into rows PostgreSQL

This is my query:这是我的查询:

SELECT * FROM profile_change_set where id = '1558079b-d954-4a0d-b241-b39fe8f3498c';
id ID table_name表名 column_key列键 column_value列值 operation_type操作类型
1558079b-d954-4a0d-b241-b39fe8f3498c 1558079b-d954-4a0d-b241-b39fe8f3498c farmers农民 smsEnabled短信启用 somvalue价值 update更新

What is want is to convert all values into rows.想要的是将所有值转换为行。

column_key列键 column_value列值
id ID 1558079b-d954-4a0d-b241-b39fe8f3498c 1558079b-d954-4a0d-b241-b39fe8f3498c
table_name表名 farmers农民
column_key列键 smsEnabled短信启用
column_value列值 somvalue价值
operation_type操作类型 update更新

Any help, thank you in advance.任何帮助,提前谢谢你。

You can use JSON features to turn columns into rows:您可以使用 JSON 功能将列转换为行:

select cols.*
from profile_change_set pcs
  cross join jsonb_each_text(to_jsonb(pcs)) as cols(column_key, column_value)
where pcs.id = ...;

Try use table function CROSSTAB (analogue PIVOT function in SQL Server)尝试使用表 function CROSSTAB (模拟 PIVOT function in Z97788240A0104CB0B55ServerC)

And take a look at this answer , in that answer, an intermediate table is used as a variable, and after that on this variable use CROSSTAB.看看这个答案,在那个答案中,一个中间表被用作变量,然后在这个变量上使用 CROSSTAB。 Take a look at this way看看这种方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM