sql server ,oracle 通用:
1. 行转列(PIVOT)
CREATE TABLE [StudentScores] INSERT INTO [StudentScores] SELECT 'alice', 'chinese', 80 |
SELECT * FROM [StudentScores] /*source table*/
AS P
PIVOT
(
SUM(Score) FOR
p.Subject IN (chinese,math,english,phy)
) AS T
2. 列转行 (UNPIVOT)
CREATE TABLE ProgrectDetail INSERT INTO ProgrectDetail |
SELECT P.ProgrectName,P.Supplier,P.SupplyNum
FROM
(
SELECT ProgrectName, OverseaSupply, NativeSupply,
SouthSupply, NorthSupply
FROM ProgrectDetail
)T
UNPIVOT
(
SupplyNum FOR Supplier IN
(OverseaSupply, NativeSupply, SouthSupply, NorthSupply )
) P