我的数据库表
StudId Day1 Day2 Day3 Day4 Day5
-------------------------------
1 P P P P P
2 A P P P P
3 P A P A P
4 A P P P P
我希望每个studentId的数据
1
Day1 P
Day2 P
Day3 P
Day4 P
Day5 P
我该如何实现这个场景?请帮帮....
答案 0 :(得分:0)
为每列使用'allion all':
假设结果列将被称为'col1'而不是'1'
Select Day1 as 'col1'
From table
Where StudId = 1
Union all
select Day2 as 'col1'
From table
Where StudId = 1
--and so on
答案 1 :(得分:0)
试,
declare @tb table (StudId int, Day1 char, Day2 char, Day3 char, Day4 char, Day5 char)
insert into @tb (StudId, Day1, Day2, Day3, Day4, Day5) values
(1,'P','P','P','P','P'),
(2,'A','P','P','P','P'),
(3,'P','A','P','A','P'),
(4,'A','P','P','P','P')
select cat,[1],[2],[3],[4] from(
select t.* from @tb cross apply( values (StudId,Day1,'day1'),(StudId,Day2,'day2'),(StudId,Day3,'day3'),(StudId,Day4,'day4'),(StudId,Day5,'day5')) t (StudId,value,cat)) t
pivot
( min(value) for studid in ([1],[2],[3],[4])
)pvt