Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
satheesh
Posting Yak Master
152 Posts |
Posted - 2012-01-11 : 05:17:22
|
| Dear All,I have a policy table and traveller table. I need to display all policy number and traveller name.Now if there are multiple travellers in a policy it gets display in separate row.But I need to display in a single rowFor egPolicy number 1212212 has 2 travellers now I am getting output like Policynumber FirstName Last Name1212212 John Helen1212212 Jermmy WaggExcepted result bePolicyNumber FirstName LastName FirstName LastName1212212 John Helen Jermmy Wagg If there are many travellers (for eg 5)then all travellers firstname and lastname need to display in separate Column in a single row Please any one can suggest me how to modify the below querySelect policy.policynumber,traveller.firstname, traveller.lastnameFrom policy inner join traveller On policy.policyid = traveller.policyidAny help regarding this is highly appreciated.Thanks Regards,SG |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
manoj_niit
Starting Member
1 Post |
Posted - 2012-01-11 : 07:12:21
|
quote: Originally posted by satheesh Dear All,I have a policy table and traveller table. I need to display all policy number and traveller name.Now if there are multiple travellers in a policy it gets display in separate row.But I need to display in a single rowFor egPolicy number 1212212 has 2 travellers now I am getting output like Policynumber FirstName Last Name1212212 John Helen1212212 Jermmy WaggExcepted result bePolicyNumber FirstName LastName FirstName LastName1212212 John Helen Jermmy Wagg If there are many travellers (for eg 5)then all travellers firstname and lastname need to display in separate Column in a single row Please any one can suggest me how to modify the below querySelect policy.policynumber,traveller.firstname, traveller.lastnameFrom policy inner join traveller On policy.policyid = traveller.policyidAny help regarding this is highly appreciated.Thanks Regards,SG
try This --Create Table STUDENTS( SubjectID Int, StudentName Varchar(100))goInsert into STUDENTSSelect 1,'Mary'Union AllSelect 1,'John'Union AllSelect 1,'Sam'Union AllSelect 2,'Alaina'Union AllSelect 2,'Edward'Select Main.SubjectID, Left(Main.Students,Len(Main.Students)-1) As "Students"From(Select distinct ST2.SubjectID, (Select ST1.StudentName + ',' AS [text()] From dbo.Students ST1 Where ST1.SubjectID = ST2.SubjectID ORDER BY ST1.SubjectID For XML PATH ('')) [Students] From dbo.Students ST2) [Main] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-11 : 07:31:07
|
quote: Originally posted by manoj_niit
quote: Originally posted by satheesh Dear All,I have a policy table and traveller table. I need to display all policy number and traveller name.Now if there are multiple travellers in a policy it gets display in separate row.But I need to display in a single rowFor egPolicy number 1212212 has 2 travellers now I am getting output like Policynumber FirstName Last Name1212212 John Helen1212212 Jermmy WaggExcepted result bePolicyNumber FirstName LastName FirstName LastName1212212 John Helen Jermmy Wagg If there are many travellers (for eg 5)then all travellers firstname and lastname need to display in separate Column in a single row Please any one can suggest me how to modify the below querySelect policy.policynumber,traveller.firstname, traveller.lastnameFrom policy inner join traveller On policy.policyid = traveller.policyidAny help regarding this is highly appreciated.Thanks Regards,SG
try This --Create Table STUDENTS( SubjectID Int, StudentName Varchar(100))goInsert into STUDENTSSelect 1,'Mary'Union AllSelect 1,'John'Union AllSelect 1,'Sam'Union AllSelect 2,'Alaina'Union AllSelect 2,'Edward'Select Main.SubjectID, Left(Main.Students,Len(Main.Students)-1) As "Students"From(Select distinct ST2.SubjectID, (Select ST1.StudentName + ',' AS [text()] From dbo.Students ST1 Where ST1.SubjectID = ST2.SubjectID ORDER BY ST1.SubjectID For XML PATH ('')) [Students] From dbo.Students ST2) [Main]
this will give all traveller details in same column as comma seperated list whereas OP wants it in different fields. Hence dynamic pivot is the way to go for which I've posted the link------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|