3
멤버 수가 미지 인 팀 테이블이있는 경우 피벗 쿼리를 동적으로 만들 수있는 방법이 있습니까?알 수없는 열과 함께 피벗 테이블을 사용할 수 있습니까?
create table #t (
team varchar (20), member varchar (20)
)
insert into #t values ('ERP', 'Jack')
insert into #t values ('ERP', 'John')
insert into #t values ('ERP', 'Mary')
insert into #t values ('ERP', 'Tim')
insert into #t values ('CRM', 'Robert')
insert into #t values ('CRM', 'Diana')
select * from #t
select team, [1] as teamMember1, /* 1st select */
[2] as teamMember2, [3] as teamMember3
from
(select team , member, row_number() /* 3rd select */
over (partition by team order by team) as rownum
from #t) a
pivot (max(member) for rownum in ([1], [2], [3])) as pvt
drop table #t
난 당신이 찾고있는 생각 : http://stackoverflow.com/questions/2922797/t-sql-pivot-possibility-of-creating 행 - 값 - 행 - 열 - 값. 동적 PIVOT을 작성하는 방법에 대한 Itzik Ben-Gan의 사례. –