如何将列转换为行或 Vica Versa
How to convert Columns into Rows or Vica Versa
我有两张桌子
学生:
Name Class Maths Science English Hindi
Sonia 2 98 67 53 58
Vijay 7 89 68 45 51
Abhishek Mishra 6 87 89 52 53
Rupal 8 74 76 59 64
Gaurav 10 90 78 43 41
主题:
Subject Total_Marks
Maths 100
Science 100
English 75
Hindi 75
当我select命名sonia时,它应该提供以下SQL输出:
Subject Total_Marks Obtained Marks
Maths 100 98
Science 100 67
English 75 53
Hindi 75 58
这有点像学校问题,但如果你怀疑的话,你应该尝试 UNPIVOT
语法而不是 PIVOT
语法。
参见 MSDN 文档以了解两者 here
使用 UNPIVOT
后跟 LEFT JOIN
,查询应该如下所示
select UP.Subject,Total_Marks, Obtained_Marks from
(
select
S.Name as Name,
S.Maths as Maths,
S.Science as Science,
S.English as English,
S.Hindi as Hindi
from Student S
where S.Name like 'sonia'
)source
UNPIVOT
(
Obtained_Marks for Subject in (Maths,Science, English, Hindi)
)UP
LEFT JOIN Subject S
ON S.Subject=UP.Subject
INSERT
用于架构的查询:
create table subject(subject varchar(10), total_marks int)
insert into subject values('Maths',100),('Science', 100),('English', 75),('Hindi', 75)
create table student(name varchar(100),class int,maths int, science int,english int,hindi int)
insert into student values
('sonia',2,98,67,53,58),
('vijay',7,89,68,45,51)
好吧,不用UNPIVOT
也能达到你想要的效果,像这样-
DECLARE @subject table (subject varchar(10), total_marks int)
DECLARE @student table (name varchar(100),class int,maths int, science int,english int,hindi int)
INSERT INTO @subject
VALUES ('Maths', 100), ('Science', 100), ('English', 75), ('Hindi', 75)
INSERT INTO @student
VALUES ('sonia', 2, 98, 67, 53, 58),
('vijay', 7, 89, 68, 45, 51)
SELECT ri.subject, ri.total_marks, le.val as ObtainedMarks FROM
(
SELECT name, class, t.sub, t.val FROM @student
CROSS APPLY (VALUES ('maths', maths), ('science', science), ('english', english), ('hindi', hindi)) AS t(sub, val)
) le
INNER JOIN
(
SELECT * FROM @subject
) ri
ON le.sub = ri.subject
WHERE le.name = 'sonia'
这里我使用了 CROSS APPLY
和 VALUES
子句来制作主题名称作为键和它的列值作为值的键值对组合。
结果
subject total_marks ObtainedMarks
-------------------------------------
Maths 100 98
Science 100 67
English 75 53
Hindi 75 58
我有两张桌子
学生:
Name Class Maths Science English Hindi
Sonia 2 98 67 53 58
Vijay 7 89 68 45 51
Abhishek Mishra 6 87 89 52 53
Rupal 8 74 76 59 64
Gaurav 10 90 78 43 41
主题:
Subject Total_Marks
Maths 100
Science 100
English 75
Hindi 75
当我select命名sonia时,它应该提供以下SQL输出:
Subject Total_Marks Obtained Marks
Maths 100 98
Science 100 67
English 75 53
Hindi 75 58
这有点像学校问题,但如果你怀疑的话,你应该尝试 UNPIVOT
语法而不是 PIVOT
语法。
参见 MSDN 文档以了解两者 here
使用 UNPIVOT
后跟 LEFT JOIN
,查询应该如下所示
select UP.Subject,Total_Marks, Obtained_Marks from
(
select
S.Name as Name,
S.Maths as Maths,
S.Science as Science,
S.English as English,
S.Hindi as Hindi
from Student S
where S.Name like 'sonia'
)source
UNPIVOT
(
Obtained_Marks for Subject in (Maths,Science, English, Hindi)
)UP
LEFT JOIN Subject S
ON S.Subject=UP.Subject
INSERT
用于架构的查询:
create table subject(subject varchar(10), total_marks int)
insert into subject values('Maths',100),('Science', 100),('English', 75),('Hindi', 75)
create table student(name varchar(100),class int,maths int, science int,english int,hindi int)
insert into student values
('sonia',2,98,67,53,58),
('vijay',7,89,68,45,51)
好吧,不用UNPIVOT
也能达到你想要的效果,像这样-
DECLARE @subject table (subject varchar(10), total_marks int)
DECLARE @student table (name varchar(100),class int,maths int, science int,english int,hindi int)
INSERT INTO @subject
VALUES ('Maths', 100), ('Science', 100), ('English', 75), ('Hindi', 75)
INSERT INTO @student
VALUES ('sonia', 2, 98, 67, 53, 58),
('vijay', 7, 89, 68, 45, 51)
SELECT ri.subject, ri.total_marks, le.val as ObtainedMarks FROM
(
SELECT name, class, t.sub, t.val FROM @student
CROSS APPLY (VALUES ('maths', maths), ('science', science), ('english', english), ('hindi', hindi)) AS t(sub, val)
) le
INNER JOIN
(
SELECT * FROM @subject
) ri
ON le.sub = ri.subject
WHERE le.name = 'sonia'
这里我使用了 CROSS APPLY
和 VALUES
子句来制作主题名称作为键和它的列值作为值的键值对组合。
结果
subject total_marks ObtainedMarks
-------------------------------------
Maths 100 98
Science 100 67
English 75 53
Hindi 75 58