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 - 表 - 列 - 從行值。 Itzik Ben-Gan關於如何構建動態PIVOT的例子。 –