CREATE TABLE [dbo].[data](
[id] [int] NULL,
[branch_id] [int] NULL,
--[branch_loc] varchar(20),
[employee] [varchar](20) NULL,
[employee_sal] decimal(32,2),
) ON [PRIMARY]
insert into [data]
select 1,1,'Muller','3000' union
select 2,1,'Meler','2000' union
select 3,1,'Schmidt','4000' union
select 4,1,'Schultz','6000' union
select 5,2,'Schroder','7000' union
select 6,2,'Thomas','8000' union
select 7,3,'Stephan','2000' union
select 8,2,'Thomas','9000'
select * from data
select branch_id,
[1_emp] as emp1, [2_emp] as emp2, [3_emp] as emp3,
[4_emp] as emp4, [5_emp]as emp5,
[1_emp_sal], [2_emp_sal], [3_emp_sal], [4_emp_sal], [5_emp_sal]
from
(
select
convert(varchar,ROW_NUMBER() over (partition by branch_id order by [id]))+'_emp' employee_branch_id,
branch_id,employee,
convert(varchar ,ROW_NUMBER() over (partition by branch_id order by [id]))+'_emp_sal' employeesalary_branch_id
,[employee_sal]
from data
) data_with_employee_branch_id
pivot
(
max(employee) --it must be a aggregat, since we have only one row the max of a string will be the string
for employee_branch_id in ( [1_emp], [2_emp], [3_emp], [4_emp], [5_emp] )
) as data_pvt1
pivot
(
max([employee_sal]) --it must be a aggregat, since we have only one row the max of a string will be the string
for employeesalary_branch_id in ( [1_emp_sal], [2_emp_sal], [3_emp_sal], [4_emp_sal], [5_emp_sal] )
) as data_pvt2
but i want the output in the below format
1 Muller 3000.00 Meler 2000.00 Schmidt 4000.00 Schultz 6000.00
2 Schroder 7000.00 Thomas 8000.00 Thomas 9000.00 NULL NULL
3 Stephan 2000.00 NULL NULL NULL NULL NULL NULL
emp5 5_emp_sal
NULL NULL
NULL NULL
NULL NULL