在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);
這些方法可以根據具體的需求和數據結構來選擇適合的方式進行列轉行操作。