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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Duplicate Rows

Author  Topic 

ALSZ37
Starting Member

25 Posts

Posted - 2014-10-09 : 18:29:24
Hi,

I am having trouble with a query returning duplicate rows. Could anyone tell me where I went wrong?

select
p.id as 'Patient ID',
l.txid as 'listing TX ID',
l.Id as 'Listing ID',
s.Org_Type,
p.firstname,
p.lastname,
zgd.name as 'Doctor Group',
p.Phone1,
p.city + ', ' + p.state as 'City,State',
CONVERT(varchar, p.birth_date, 101) AS 'DOB',
(select [dbo].[getageyears](p.Birth_Date,getdate()) as patient_age) as 'Age',
p.sex as 'Sex ID',
zs.name as 'Sex',
(select [dbo].[GetPatientHeight] (rd.Pa_Id)) as 'Height cm',
(select [dbo].[GetPatientWeight] (rd.pa_id)) as 'Weight kg',
cast(rd.weight * 2.205 as decimal(9,1)) as 'Weight lbs',
(select CONVERT(varchar, l.list_date, 101) where l.centerid = 276) as 'Center1 List Date',
(select CONVERT(varchar, l.list_date, 101) where l.centerid = 345) as 'Center2 List Date',
(select DATEDIFF(day,l.list_date,getdate()) where l.centerid = 345) as 'Center2 Wait List Days',
lhu.UNOScPRA,
p.BloodType,
zbt.name as 'Blood Type',
l.disposition,
zl.name as 'Status',
l.centerid,
zltc.name as 'Listing Center',
l.remarks as 'Notes'
from Patient p
join TX_Shared_Main s on p.id = s.pa_id
join TX_REC_Listing l on l.txid = s.id
join Z_BloodType zbt on zbt.id = p.BloodType
join TX_Z_Listing_Disposition zl on zl.id = l.disposition
join TX_Lookup_Transplant_Centers zltc on zltc.id = l.centerid
join Z_Sex zs on zs.Id = p.sex
join Rdv_Detail rd on rd.Pa_Id = p.Id
left join doctor d on d.id = p.Dr_Id
left join patient_Reference pr on pr.aux_id = d.id
left join Group_Doctor gd on gd.dr_id = d.id
left join Group_Z_Doctor zgd on zgd.id = gd.group_id
left join TX_REC_Histo_UNOScPRA lhu on lhu.pa_id=p.Id
where p.InUse =1 and l.InUse= 1 and s.InUse = 1 and p.Birth_Date is not null
and s.Org_Type = 5
and l.disposition = 1

Lines 1 and 3 are the same and lines 2 and 4 are the same.


[URL=http://smg.photobucket.com/user/lezgettdrunk/media/10-9-20145-16-27PM_zpsec053f76.jpg.html][/URL]

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-10-10 : 09:21:34
First off, use a formatter! Try poorsql.com, which reformats your query like this:


SELECT p.id AS 'Patient ID'
,l.txid AS 'listing TX ID'
,l.Id AS 'Listing ID'
,s.Org_Type
,p.firstname
,p.lastname
,zgd.NAME AS 'Doctor Group'
,p.Phone1
,p.city + ', ' + p.STATE AS 'City,State'
,CONVERT(VARCHAR, p.birth_date, 101) AS 'DOB'
,(
SELECT [dbo].[getageyears](p.Birth_Date, getdate()) AS patient_age
) AS 'Age'
,p.sex AS 'Sex ID'
,zs.NAME AS 'Sex'
,(
SELECT [dbo].[GetPatientHeight](rd.Pa_Id)
) AS 'Height cm'
,(
SELECT [dbo].[GetPatientWeight](rd.pa_id)
) AS 'Weight kg'
,cast(rd.weight * 2.205 AS DECIMAL(9, 1)) AS 'Weight lbs'
,(
SELECT CONVERT(VARCHAR, l.list_date, 101)
WHERE l.centerid = 276
) AS 'Center1 List Date'
,(
SELECT CONVERT(VARCHAR, l.list_date, 101)
WHERE l.centerid = 345
) AS 'Center2 List Date'
,(
SELECT DATEDIFF(day, l.list_date, getdate())
WHERE l.centerid = 345
) AS 'Center2 Wait List Days'
,lhu.UNOScPRA
,p.BloodType
,zbt.NAME AS 'Blood Type'
,l.disposition
,zl.NAME AS 'Status'
,l.centerid
,zltc.NAME AS 'Listing Center'
,l.remarks AS 'Notes'
FROM Patient p
INNER JOIN TX_Shared_Main s ON p.id = s.pa_id
INNER JOIN TX_REC_Listing l ON l.txid = s.id
INNER JOIN Z_BloodType zbt ON zbt.id = p.BloodType
INNER JOIN TX_Z_Listing_Disposition zl ON zl.id = l.disposition
INNER JOIN TX_Lookup_Transplant_Centers zltc ON zltc.id = l.centerid
INNER JOIN Z_Sex zs ON zs.Id = p.sex
INNER JOIN Rdv_Detail rd ON rd.Pa_Id = p.Id
LEFT JOIN doctor d ON d.id = p.Dr_Id
LEFT JOIN patient_Reference pr ON pr.aux_id = d.id
LEFT JOIN Group_Doctor gd ON gd.dr_id = d.id
LEFT JOIN Group_Z_Doctor zgd ON zgd.id = gd.group_id
LEFT JOIN TX_REC_Histo_UNOScPRA lhu ON lhu.pa_id = p.Id
WHERE p.InUse = 1
AND l.InUse = 1
AND s.InUse = 1
AND p.Birth_Date IS NOT NULL
AND s.Org_Type = 5
AND l.disposition = 1


Second, if you're getting duplicate rows, you're probably missing a join condition but without knowing your data, there's no way to tell. try running your query bit by bit to see where the duplicates show up. that is, start with the select...from and the first join. Look for duplicates in the output. If none, add the second join and repeat. When you find the duplicates, look at the source data and the join conditions. Figure out which one is missing and add it in.

As a last resort, use DISTINCT. I say as a last resort, since this forces SQL to sort the result set. Best to avoid that cost if you can.
Go to Top of Page
   

- Advertisement -