Possible Duplicate:
combine multiple rows int one row with many to many
Using SQL Server I have the following tables/ data
CUS_VISIT
Cus_ID Cus_Vis_ID
1 01
1 02
2 01
and
CUS_EVENT
Cus_Event_ID Cus_ID Cus_Vis_ID
001 1 01
002 1 01
and
CUS_XREF
Cus_ID Cus_Event_ID Cus_Prt_ID Cus_Seq_No
1 001 1 1
1 001 2 1
and
CUS_PRT
Cus_ID Cus_Prt_ID Prt_Cd
1 1 1A
1 2 2A
I am trying to get the following
SQL RESULTS
Cus_ID Prt_Cd Cus_Vis_ID
1 1A,2A 1
what I end up with is
SQL RESULTS
Cus_ID Prt_Cd Cus_Vis_ID
1 1A 1
1 2A 1
The tables are linked by ...
CUS_VISIT.Cus_ID = CUS_EVENT.Cus_ID
CUS_VISIT.Cus_Vis_ID = CUS_EVENT.Cus_Vis_ID
CUS_VISIT.Cus_ID = CUS_XREF.Cus_ID
CUS_EVENT.Cus_Event_ID = CUS_XREF.Cus_Event_ID
CUS_XREF.Cus_Prt_ID = CUS_PRT.Cus_Prt_ID
CUS_XREF.Cus_ID = CUS_PRT.Cus_ID
I can almost get what I am after if I drop the CUS_XREF.Cus_Prt_ID = CUS_PRT.Cus_Prt_ID join but then I get all of the part codes (Prt_Cd) for the customer not just the ones for that visit.
here is what i have
select distinct CUS_EVENT.cus_id, CUS_EVENT.cus_visit_id,
(Select CUS_PRT.prt_cd + ',' AS [text()]
From CUS_PRT, CUS_XREF
where
CUS_EVENT.cus_id=XREF.cus_id
and CUS_EVENT.cus_event_id = XREF.cus_event_id
and CUS_XREF.cus_id=CUS_PRT.cus_id
and CUS_XREF.cus_prt_id = CUS_PRT.cus_prt_id
and CUS_XREF.prt_seq_no ='1'
order by CUS_PRT.prt_cd
for XML PATH('')) [Codes]
from CUS_EVENT
i made a similar post recently but didn't get any specific help. i think i need another sub-query some where. thanks for looking at this.