student | group
1 | A
2 | B
1 | B
3 | C
我想生成如下输出:
Student | Group_A | Group_B | Group_C
1 | Yes | Yes |
2 | | Yes |
3 | | | Yes
有人知道我该如何制作这类报告?我尝试了几种使用Pivot和Unpivot的方法,但它没有在这里工作
答案 0 :(得分:1)
我相信你正在寻找支点。
http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
declare @students table(id int, type_ nvarchar)
insert into @students
values(1, 'A'),
(2, 'B'),
(3, 'C'),
(1, 'B');
select result.id Student,
iif(result.A = 1, 'Yes', 'No') Group_A,
iif(result.B = 1, 'Yes', 'No') Group_B,
iif(result.C = 1, 'Yes', 'No') Group_C
from (
select *
from @students a
pivot(count(type_) for type_ in (
[A],
[B],
[C]
)) as pivotExample
) result;
如果需要动态支点,我会推荐你: