在 Oracle 中将多行数据转换为一行多列可以使用 LISTAGG
函数或者 PIVOT
操作。
LISTAGG
函数:SELECT LISTAGG(column_name, ', ') WITHIN GROUP (ORDER BY column_order) AS concatenated_values
FROM table_name;
例如,将 table_name
中的 column_name
列的多行数据转换为一行多列数据:
SELECT LISTAGG(column_name, ', ') WITHIN GROUP (ORDER BY column_order) AS concatenated_values
FROM table_name;
PIVOT
操作:SELECT *
FROM (
SELECT column_name, column_value
FROM table_name
)
PIVOT (
MAX(column_value)
FOR column_name IN ('value1' AS value1, 'value2' AS value2, 'value3' AS value3)
);
例如,将 table_name
中的 column_name
列的多行数据转换为一行多列数据:
SELECT *
FROM (
SELECT column_name, column_value
FROM table_name
)
PIVOT (
MAX(column_value)
FOR column_name IN ('value1' AS value1, 'value2' AS value2, 'value3' AS value3)
);
在以上示例中,根据实际情况修改 table_name
、column_name
和 column_value
等参数即可将多行数据转换为一行多列数据。