Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2005 在SQLServer2005中多次透视表以包含同一列_Sql Server 2005_Dynamic Pivot - Fatal编程技术网

Sql server 2005 在SQLServer2005中多次透视表以包含同一列

Sql server 2005 在SQLServer2005中多次透视表以包含同一列,sql-server-2005,dynamic-pivot,Sql Server 2005,Dynamic Pivot,我需要透视名为tblGameRoleName的下表- Game Role Name VolleyBall Coach Sujatha VolleyBall Player Rajendran VolleyBall Player Juno VolleyBall Player Indira VolleyBall Player Ganesh VolleyBall Player Vasanth Tennis Coach Rajeshkumar T

我需要透视名为tblGameRoleName的下表-

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 游戏教练玩家1玩家2玩家3玩家4玩家5 排球运动员苏贾塔·拉金德兰·朱诺·英迪拉·加内什·瓦桑斯 网球选手拉杰什库马尔·维韦克·鲁巴拉无效无效无效 问题是不同的“游戏”中“玩家”的数量可能会增加,结果表应该显示所有游戏的所有玩家。例如,如果我将以下“板球”队添加到此表中-

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个player列


这可以通过枢轴功能实现吗?如果没有,请建议实现结果表的正确方法。

这在前端报告/显示应用程序中可能更容易,但对于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

你用什么标准来安排球员?没有标准来安排球员。顺序一点也不重要。任何游戏的第一个玩家记录可以作为Player1出现,下一个玩家记录可以作为Player2出现,依此类推。如果必须按顺序才能获得结果表,则可以按字母顺序对玩家进行排序。
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