Solved

Join statement is causing too many results...

Posted on 2011-02-11
4
293 Views
Last Modified: 2012-05-11
I have a select statement that is joining to tables.

In the ConsolidatedBOLReports Table there is 1 record per ConsolidatedProNumber.  In the conbol table there is multiple records per consolidatedProNumber.  

I am using this query to show the report from the consolidatedbolreports table but it is returning 8 rows when I only want it to return 1.  I know it is returning multiple rows because the join is causing it to return a row for each record in the joining table which in this case is conbol.  How can I get it to only return 1 row?

Thanks.
select r.id,r.consolidatedpronumber,r.PlantId,r.carrier, c.tocompany as ShipToCompany, c.toaddr1 as ShipToAdd1,
c.tostate as ShipToState,c.tocity as ShipToCity,c.tozip as ShipToZipCode,c.conbolid as ConBol    
from consolidatedbolreports r join conbol c 
on r.consolidatedpronumber = c.conproid and r.Carrier=c.Carrier    
where consolidatedpronumber in (select conproid from conbol ) 
and r.consolidatedpronumber='123456' and r.PlantId='PLANT001' and r.carrier='Conway'

Open in new window

0
Comment
Question by:Bran-Damage
4 Comments
 
LVL 32

Accepted Solution

by:
ewangoya earned 250 total points
ID: 34872941
did you try using distinct
select distinct r.id,r.consolidatedpronumber,r.PlantId,r.carrier, c.tocompany as ShipToCompany, c.toaddr1 as ShipToAdd1,
c.tostate as ShipToState,c.tocity as ShipToCity,c.tozip as ShipToZipCode,c.conbolid as ConBol    
from consolidatedbolreports r join conbol c 
on r.consolidatedpronumber = c.conproid and r.Carrier=c.Carrier    
where consolidatedpronumber in (select conproid from conbol ) 
and r.consolidatedpronumber='123456' and r.PlantId='PLANT001' and r.carrier='Conway'

Open in new window

0
 
LVL 9

Assisted Solution

by:sshah254
sshah254 earned 250 total points
ID: 34872966
Obviously since it is returning 8 records, there is different data in the conbol table.

You can try this if it works in your situation ... select distinct r.id, r.consolidatepronumber ...

If the data c.tocompany, c.toaddr1, c.tostate1, c.tocity, c.tozip, c.conbolid is the same for all the 8 records, then it will work.

If not, you'll need to decide which record you want to select.

Come back to the forum and let us know if you need more help.

Ss
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34873099
please read this article: http://www.experts-exchange.com/A_3203.html
it explains the issue and solutions.
0
 

Author Closing Comment

by:Bran-Damage
ID: 34874726
thanks guys, distinct slipped my mind on that one.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now