Try this . . .create table #Location( LocationID int, LocationName varchar(30))create table #Person( PersonID int, Name varchar(30))create table #Designation( DesignationID int, Description varchar(30))create table #LocationPerson( LocationPersonID int, LocationID int, PersonID int)create table #LocationPersonDesignation( LocationPersonDesignationID int, LocationPersonID int, DesignationID int)insert into #Locationselect 1, '1001' union allselect 2, '1002'insert into #Personselect 1, 'ABC' union allselect 2, 'XYZ' union allselect 3, 'PQR'insert into #Designationselect 1, 'Manager' union allselect 2, 'Asst.Manager'insert into #LocationPersonselect 1, 1, 1 union allselect 2, 2, 2 union allselect 3, 1, 3insert into #LocationPersonDesignationselect 1, 1, 1 union allselect 2, 2, 1 union allselect 3, 3, 2select l.LocationName, max(case when lpd.DesignationID = 1 then p.Name else '' end) as 'Manager', max(case when lpd.DesignationID = 2 then p.Name else '' end) as 'Asst. Manager'from #LocationPersonDesignation lpd inner join #LocationPerson lp on lpd.LocationPersonID = lp.LocationPersonID inner join #Location l on lp.LocationID = l.LocationID inner join #Person p on lp.PersonID = p.PersonIDgroup by l.LocationNamedrop table #Locationdrop table #Persondrop table #Designationdrop table #LocationPersondrop table #LocationPersonDesignation
In future, try to post your tables structure & data like this. It is easier for us to test the codes-----------------'KH'Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.