Điều này có thể dễ dàng hơn trong ứng dụng báo cáo/hiển thị giao diện người dùng nhưng đối với sql bạn cần thực hiện trục động. Nhưng vì các cột được đặt bí danh bởi số trình phát theo trình tự và các trình phát cụ thể khác nhau theo trò chơi, bạn không thể sử dụng các ví dụ sql động kiểu chữ.
Dưới đây là một cách để làm điều đó:
dữ liệu mẫu
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'
tạo động SELECT và PIVOT khoản và báo cáo kết quả 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
Bạn sử dụng tiêu chí nào hoặc der các cầu thủ? –
Không có tiêu chí để đặt hàng người chơi. Thứ tự không quan trọng chút nào. Hồ sơ người chơi đầu tiên cho bất kỳ trò chơi nào có thể đến như Player1 và bản ghi cầu thủ tiếp theo có thể đến như Player2 và cứ tiếp tục như vậy. Nếu một đơn đặt hàng là bắt buộc để đạt được bảng kết quả, thì người chơi có thể được sắp xếp theo thứ tự bảng chữ cái. – MediumOne