在Oracle中,可以使用PIVOT和UNPIVOT命令來實現行列轉換。
SELECT *
FROM (
SELECT product_type, sales_amount
FROM sales_data
)
PIVOT
(
SUM(sales_amount)
FOR product_type IN ('A' AS A_sales, 'B' AS B_sales, 'C' AS C_sales)
);
SELECT product_type, sales_amount
FROM (
SELECT A_sales, B_sales, C_sales
FROM sales_data
)
UNPIVOT
(
sales_amount FOR product_type IN (A_sales AS 'A', B_sales AS 'B', C_sales AS 'C')
);
通過PIVOT和UNPIVOT命令,可以實現行列轉換的需求。