Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

join 2 tables without duplicate rows Expand / Collapse
Author
Message
Posted Thursday, January 2, 2014 2:04 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, August 22, 2014 7:14 AM
Points: 96, Visits: 159
Having a bit of a brain freeze at the moment!

I wish to extract data from 2 tables which are linked by dbPatID but don't want duplicate rows created when there is more than one occurrence of dbPatID in table 2 which is what this sql is bringing back.


Select EPSAgedBalances.dbPatID, EPSAgedBalances.dbPatFirstName, EPSAgedBalances.dbPatLastName,
EPSAllContracts.dbStaffLastName, EPSAgedBalances.TotTot
from EPSAgedBalances
inner join EPSAllContracts on EPSAllContracts.dbPatID = EPSAgedBalances.dbPatID
where EPSAgedBalances.TotTot < 0


In other words I'm just after the dbStaffLastName from table 2 that matches up with dbPatID.

thanks
Post #1527006
Posted Thursday, January 2, 2014 2:18 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Friday, September 19, 2014 3:37 AM
Points: 708, Visits: 3,288
Happy new year!

So one contract can have multiple aged balances?

How about the below:

Select b.dbPatID, a.dbPatFirstName, a.dbPatLastName, b.dbStaffLastName, SUM(a.TotTot) AS TotTot
from EPSAgedBalances AS a
inner join EPSAllContracts AS b
on b.dbPatID = a.dbPatID
GROUP BY b.dbPatID, a.dbPatFirstName, a.dbPatLastName, b.dbStaffLastName
HAVING SUM(a.TotTot) < 0



---------------------------------------------------------


It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens

Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
Post #1527007
Posted Thursday, January 2, 2014 2:30 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 3:19 AM
Points: 6,721, Visits: 13,832
Here's an alternative to Abu Dinas' suggestion;
SELECT 
b.dbPatID,
b.dbPatFirstName,
b.dbPatLastName,
x.dbStaffLastName,
b.TotTot
FROM EPSAgedBalances b
CROSS APPLY (
SELECT TOP 1 dbStaffLastName
FROM EPSAllContracts c
WHERE c.dbPatID = b.dbPatID
) x
WHERE b.TotTot < 0

Not necessarily better - you should try both and compare performance.


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1527011
Posted Thursday, January 2, 2014 3:01 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, August 22, 2014 7:14 AM
Points: 96, Visits: 159
Hi Guys, thanks for your help. I found simply adding distinct has done the trick


Select distinct EPSAgedBalances.dbPatID, EPSAgedBalances.dbPatFirstName, EPSAgedBalances.dbPatLastName,
EPSAllContracts.dbStaffLastName, EPSAgedBalances.TotTot
from EPSAgedBalances, EPSAllContracts
where EPSAllContracts.dbPatID = EPSAgedBalances.dbPatID
and TotTot < 0


thanks
Post #1527017
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse