the following shows my sample dataset
PatientID PatientName XXX-037070002 Riger, Jens^Wicki XXX-037070002 Riger^Wicki XXX-10052 Weier,Nicole^Peggy XXX-10052 Weier,Nicole^Peppy XXX-23310 Rodem^Sieglinde XXX-23310 Sauberger, Birgit^Finja XXX-23343 Je, Ronny^Wilma XXX-23343 Jer, Ronny^Wilma XXX-2349 Kel,Andy^Juka XXX-2349 Kel^Juka XXX-2998 Hel, Frank XXX-2998 Hel,Frank^Fenris XXX-3188 Mey, Marion XXX-3188 Mey, Marion^Paula XXX-3188 Schulz^Roma XXX-3218 Böntgen-Simnet,Dr. Regine^Cara XXX-3218 Simnet,Dr. Regine^Cara XXX-3826 Mertes, Bernd Uwe^Ellie XXX-3826 Mertes,Bernd^Ellie XXX-3826 Mertes^Ellie This is the query I got from my last request:
with d as ( select distinct patid, patname from dicomstudys ) select * from d where d.patid in ( select d.patid from d group by d.patid having count(*) > 1 ) Now I want to adjust the query that only the following data get's an output:
PatientID PatientName XXX-23310 Rodem^Sieglinde XXX-23310 Sauberger, Birgit^Finja XXX-23343 Je, Ronny^Wilma XXX-23343 Jer, Ronny^Wilma XXX-3188 Mey, Marion XXX-3188 Mey, Marion^Paula XXX-3188 Schulz^Roma XXX-3218 Böntgen-Simnet,Dr. Regine^Cara XXX-3218 Simnet,Dr. Regine^Cara Last names are either seperated with a ',' or '^' . If last names are the same for the same PatientID then I dont want them being displayed. I tried fiddling with a sub select statement featuring a combination of CHARINDEX commands and others but my SQL syntax knowledge is very limited with the complexity of the request.
Please also note that for the case for XXX-3188 has two datasets with the same last name but also another dataset with a complete new patientName and thus it needs to be in the output.
