2010-01-21 21 views
5

我需要轉動下表命名tblGameRoleName -樞軸在SQL Server 2005中的表包含相同的列多次

 

Game  Role Name 
VolleyBall Coach Sujatha 
VolleyBall Player Rajendran 
VolleyBall Player Juno 
VolleyBall Player Indira 
VolleyBall Player Ganesh 
VolleyBall Player Vasanth 
Tennis  Coach Rajeshkumar 
Tennis  Player Vivek 
Tennis  Player Rubala 

下表它有「球員」列多次 -

 

Game  Coach  Player1  Player2 Player3 Player4 Player5 
VolleyBall Sujatha  Rajendran Juno Indira Ganesh Vasanth 
Tennis  Rajeshkumar Vivek  Rubala NULL NULL NULL 

問題是,不同的'遊戲'會增加'玩家'的數量,結果表應該顯示所有遊戲的所有玩家。例如 - 如果我在此表中添加以下「板球」球隊 -

 

Cricket Coach Gary 
Cricket Player Viru 
Cricket Player Gauti 
Cricket Player Sachin 
Cricket Player Mahi 
Cricket Player Yuvi 
Cricket Player Suresh 
Cricket Player Virat 
Cricket Player Bhajji 
Cricket Player Zaheer 
Cricket Player Ishant 
Cricket Player Ashish 

然後結果表應顯示11個選手列。

這可以通過PIVOT功能來實現嗎?如果沒有,請建議正確的方法來實現結果表。

+0

你使用什麼標準或球員? – 2010-01-21 14:52:50

+0

沒有標準來訂購球員。順序並不重要。任何遊戲的第一個玩家記錄可以來自玩家1,而下一個玩家記錄可以來自玩家2等等。如果命令是強制性的以獲得結果表,則玩家可以按字母順序排序。 – MediumOne 2010-01-21 17:54:29

回答

3

這可能在前端報告/顯示應用程序中更容易,但是對於需要執行動態數據透視表的sql。但是因爲這些列是由連續玩家編號造成的,並且特定玩家因遊戲而異,所以您不能使用典型的動態sql示例。

下面是做到這一點的一種方法:

樣本數據

set ansi_warnings off 
set nocount on 
create table #t (Game varchar(20), Role varchar(15), [Name] varchar(20)) 
insert #t 
      select 'VolleyBall', 'Coach', 'Sujatha' 
union all select 'VolleyBall', 'Player', 'Rajendran' 
union all select 'VolleyBall', 'Player', 'Juno' 
union all select 'VolleyBall', 'Player', 'Indira' 
union all select 'VolleyBall', 'Player', 'Ganesh' 
union all select 'VolleyBall', 'Player', 'Vasanth' 
union all select 'Tennis', 'Coach', 'Rajeshkumar' 
union all select 'Tennis', 'Player', 'Vivek' 
union all select 'Tennis', 'Player', 'Rubala' 
union all select 'Cricket', 'Coach', 'Gary' 
union all select 'Cricket', 'Player', 'Viru' 
union all select 'Cricket', 'Player', 'Gauti' 
union all select 'Cricket', 'Player', 'Sachin' 
union all select 'Cricket', 'Player', 'Mahi' 
union all select 'Cricket', 'Player', 'Yuvi' 
union all select 'Cricket', 'Player', 'Suresh' 
union all select 'Cricket', 'Player', 'Virat' 
union all select 'Cricket', 'Player', 'Bhajji' 
union all select 'Cricket', 'Player', 'Zaheer' 
union all select 'Cricket', 'Player', 'Ishant' 
union all select 'Cricket', 'Player', 'Ashish' 

創建動態SELECT和PIVOT子句和EXEC'd聲明

declare @max int 
select top 1 @max = count(*) 
from #t 
where role = 'player' 
group by game 
order by count(*) desc 

declare @sel varchar(2000) 
     ,@piv varchar(2000) 

;with nos (n) as (select 1 union all select n+1 from nos where n < @max) 
select @sel = coalesce(@sel + ', ' 
     + 'max([' + convert(varchar(2), n) + ']) as player' + convert(varchar(2), n) 
     , 'max([' + convert(varchar(2), n) + ']) as player' + convert(varchar(2), n) 
     ) 

     ,@piv = coalesce(@piv + ',[' + convert(varchar(2), n) + ']', '[' + convert(varchar(2), n) + ']') 
from nos 
----------------------------------------------------------------------------- 

exec(' 
select p.game 
     ,max(p.coach) as coach 
     ,' + @sel + ' 
from (
     select game 
       ,case when role = ''coach'' then [name] end as coach 
       ,case when role = ''player'' then [name] end as player 
       ,row_number() over (partition by game, role order by name) as seq 
     from #t 
     ) d 
pivot (max(player) for seq in (' + @piv + ')) p 
group by p.game 
') 

go 
drop table #t 

OUTPUT:

game     coach    player1    player2    player3    player4    player5    player6    player7    player8    player9    player10    player11 
-------------------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- 
Cricket    Gary     Ashish    Bhajji    Gauti    Ishant    Mahi     Sachin    Suresh    Virat    Viru     Yuvi     Zaheer 
Tennis    Rajeshkumar   Rubala    Vivek    NULL     NULL     NULL     NULL     NULL     NULL     NULL     NULL     NULL 
VolleyBall   Sujatha    Ganesh    Indira    Juno     Rajendran   Vasanth    NULL     NULL     NULL     NULL     NULL     NULL 
+0

謝謝!有用! – MediumOne 2010-01-22 11:58:37