I am trying to make a table that shows all the patients checked in to the hospital. I can join client, patient, check-in, appointment data just fine, but the alerts table has multiple rows which I am trying to aggregate/concatenate/rollup. I tried to create an XML statement but it doesn't seem to be working. I would like for all the alerts for the patient to be a single comma-separated string in one row. here is what I have:
select DISTINCT
a.ResourceAbbreviation1, a.AppointmentType, a.StatusNum, c.sLastName,
pt.Name, pt.WeightString, pt.AgeShort, pt.Breed, pt.Species, pt.Gender, pt.NewPatient,
(select SUBSTRING((
select ',' + al.stext AS 'data()'
FOR XML PATH('')
), 2, 9999) as cautions),
pt.Classification, p.kPatientId
from dbo.entpatients pt
join alerts al
on al.kpatientid = pt.IDPatient
join dbo.PatientCheckIns P
on pt.IDPatient=p.kPatientId
join dbo.EntAppointments a
on a.IDPatient = p.kPatientId
join dbo.clients c
on c.kID=a.IDClient
where cast (a.StartTime as date) = cast(getdate() as date)
and a.StatusNum=4;