Solved

Join statement is causing too many results...

Posted on 2011-02-11
4
331 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:
Ephraim Wangoya 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 143

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

730 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