首页 > 代码库 > SQL: PIVOT 数据透视 行列转换
SQL: PIVOT 数据透视 行列转换
-- 数据透视
-- PIVOT: 行转列
SELECT * FROM
(
SELECT N‘张三‘ AS 姓名, N‘语文‘ AS 课程,70 AS 分数 UNION
SELECT N‘张三‘ AS 姓名, N‘数学‘ AS 课程,90 AS 分数 UNION
SELECT N‘李四‘ AS 姓名, N‘语文‘ AS 课程,85 AS 分数 UNION
SELECT N‘李四‘ AS 姓名, N‘数学‘ AS 课程,85 AS 分数
) AS A
PIVOT
(
MAX(分数)
FOR 课程
IN(语文, 数学)
) AS B-- UNPIVOT: 列转行
SELECT 姓名, 课程, 分数 FROM
(
SELECT N‘张三‘ AS 姓名, 70 AS 语文, 90 AS 数学 UNION
SELECT N‘李四‘ AS 姓名, 85 AS 语文, 85 AS 数学
) AS B
UNPIVOT
(
分数
FOR 课程
IN(语文, 数学)
) AS A
SQL: PIVOT 数据透视 行列转换
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。