SQLServer 的case when 行转列 列转行的使用

SQL 行转列 列转行
Mysql 也适用 case when方式 case 对应列 when 值 判断 then yes else no
(my sql 不适用)可以使用 PIVOT,UNPIVOT比较快速实现行转列,列转行

行转列
SELECT * FROM [StudentScores]
AS P
PIVOT
(
SUM(Score/行转列后 列的值/) FOR
p.Subject/需要行转列的列/ IN ([语文],[数学],[英语],[生物]/列的值/)
) AS T

列转行

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

mySQl不适用

你可能感兴趣的:(SQL)