Solved

Join statement is causing too many results...

Posted on 2011-02-11
4
338 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
[X]
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
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

691 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