Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Join statement is causing too many results...

Posted on 2011-02-11
4
Medium Priority
?
342 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 1000 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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

618 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