在PostgreSQL中,将列转换为行的方法有以下几种:
SELECT id, type, value
FROM your_table
UNPIVOT (value FOR type IN (col1, col2, col3)) AS unpivoted_data;
SELECT id, 'col1' AS type, col1 AS value FROM your_table
UNION ALL
SELECT id, 'col2' AS type, col2 AS value FROM your_table
UNION ALL
SELECT id, 'col3' AS type, col3 AS value FROM your_table;
SELECT *
FROM CROSSTAB(
'SELECT id, type, value FROM your_table',
'SELECT DISTINCT type FROM your_table'
) AS ct(id int, col1 text, col2 text, col3 text);
这些方法可以根据具体的需求和数据结构来选择适合的方式进行列转行操作。