EDIT
Sorry, I have not made a very good job of explaining myself.
The reason I am using a UNION is due to pulling this data from a form in the database. That form is made up of multiple different questions and answers, so depending on the question - I need to join to the same tables, but using different links.
So below is a better example
select ra.AgreementId, sub.Name
from RentAgreement ra
left join
(
--Dogs
Select AA.Reference, AA.ApplicationId,AA.ContactGroupId,e.AgreementId, CONCAT(u.Description,' (Dog)') 'Name'
from AllocationApplication aa
inner join ContactGroupLink l on l.ContactGroupId = aa.ContactGroupId and l.EffectiveToDate is null
inner join RentAgreementEpisode e on e.AgreementEpisodeId = l.LinkId and l.LinkTypeId = 2
inner join SystemFormResponse sfr on sfr.FormId = aa.ApplicationId AND sfr.FormTemplateId = 5 /*(systemformtype 5 = Pet Register)*/
inner join SystemFormPage p on p.FormTemplateId = sfr.FormTemplateId AND p.FormPageId = 6 /*(Pet Register Page)*/
inner join SystemFormSection s on s.FormPageId = p.FormPageId AND s.FormSectionId = 6 /*Pet Details */
Inner join SystemFormGroup g ON g.FormSectionId = s.FormSectionId and FormGroupId in (16)
inner join SystemFormQuestion q on q.FormGroupId = g.FormGroupId and q.EffectiveToDate is null and q.FormQuestionId in (119) /*DogBreed*/
inner join SystemFormQuestionSetup qs on qs.FormQuestionSetupId = q.FormQuestionSetupId
inner join SystemFormResponseAnswer a on a.FormQuestionId = q.FormQuestionId and a.FormResponseId = sfr.FormResponseId
inner join SystemFormResponseDetail d on d.FormResponseAnswerId = a.FormResponseAnswerId
where aa.ApplicationTypeId = 1
union all
--Cats
Select AA.Reference, AA.ApplicationId,AA.ContactGroupId,e.AgreementId, CONCAT(d.TextResponse,' (Cat)') 'Name'
from AllocationApplication aa
inner join ContactGroupLink l on l.ContactGroupId = aa.ContactGroupId and l.EffectiveToDate is null
inner join RentAgreementEpisode e on e.AgreementEpisodeId = l.LinkId and l.LinkTypeId = 2
inner join SystemFormResponse sfr on sfr.FormId = aa.ApplicationId AND sfr.FormTemplateId = 5 /*(systemformtype 5 = Pet Register)*/
inner join SystemFormPage p on p.FormTemplateId = sfr.FormTemplateId AND p.FormPageId = 6 /*(Pet Register Page)*/
inner join SystemFormSection s on s.FormPageId = p.FormPageId AND s.FormSectionId = 6 /*Pet Details */
Inner join SystemFormGroup g ON g.FormSectionId = s.FormSectionId and FormGroupId in (17)
inner join SystemFormQuestion q on q.FormGroupId = g.FormGroupId and q.EffectiveToDate is null and q.FormQuestionId = 132 /*CatName*/
inner join SystemFormResponseAnswer a on a.FormQuestionId = q.FormQuestionId and a.FormResponseId = sfr.FormResponseId
inner join SystemFormResponseDetail d on d.FormResponseAnswerId = a.FormResponseAnswerId
where aa.ApplicationTypeId = 1
) sub on sub.AgreementId = ra.AgreementId
where ra.AgreementId = 1775
What that does is pull me back the dogs and cats in a property. The result set looks like below.
So this particular Agreementid - has 2 dogs (the first two row pulled from the first UNION and 1 cat. Depending on the type of pet - for a dog they want the breed to be returned - for a cat, just the name of the animal.
What I want to achieve from the code below is just two columns and 1 row. Column 1 would be the AgreementId and the second column would be name, but with all three pets in this example on one comma separated line like - Lhasa Apso (Dog), Pug (Dog), PussPuss (Cat)
Thank you very much