declare @table as table(id int,name varchar(10))
insert into @table values(1, 'gidi1')
insert into @table values(1 , 'gidi2')
insert into @table values(1 , 'gidi3')
insert into @table values(2 , 'gidi4')
insert into @table values(2 , 'gidi5')
select id,
max(case rn when 1 then name end) as name1,
max(case rn when 2 then name end) as name2,
max(case rn when 3 then name end) as name3
from (select *,row_number() over (partition by id order by name) as rn
from @table) as t1
group by id
/*
1 gidi1 gidi2 gidi3
2 gidi4 gidi5 NULL
*/
|