Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Join statement is causing too many results...

Posted on 2011-02-11
4
324 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 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

829 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