1. 程式人生 > >SQL語句中列轉行案例

SQL語句中列轉行案例

--初始化示例
DECLARE @StudentGrade TABLE(student VARCHAR(20), chinese int, maths int, english int)

INSERT @StudentGrade(student,chinese,maths,english)
VALUES('張三','98','76','89')

INSERT @StudentGrade(student,chinese,maths,english)
VALUES('李四','100','74','76')

INSERT @StudentGrade(student,chinese,maths,english)
VALUES('王五','87','90','71')

--原表查詢
SELECT * FROM @StudentGrade

--列轉行後查詢
SELECT student,scoure,grade
FROM @StudentGrade
unpivot(grade FOR scoure in(chinese,maths,english))AS up

--結果如圖