在Oracle中,可以使用PIVOT函數將多行數據轉換為多列數據。以下是一個示例:
假設有一個名為sales的表,包含以下數據:
region | product | sales_amount |
---|---|---|
North | A | 100 |
North | B | 200 |
North | C | 150 |
South | A | 120 |
South | B | 250 |
South | C | 180 |
要將上述數據轉換為多列,可以使用以下SQL查詢:
SELECT *
FROM (
SELECT region, product, sales_amount
FROM sales
)
PIVOT (
SUM(sales_amount)
FOR product IN ('A' AS A, 'B' AS B, 'C' AS C)
)
ORDER BY region;
執行以上查詢將得到以下輸出:
REGION | A | B | C |
---|---|---|---|
North | 100 | 200 | 150 |
South | 120 | 250 | 180 |
這樣就將原始的多行數據轉換為了多列數據。