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

x
?
Solved

Left Outer Join with only 1 result

Posted on 2013-01-18
7
Medium Priority
?
373 Views
Last Modified: 2013-01-18
I want to run a sql query using a left outer join, but I only want to return the first result, not all.

Table1:
ParentID 2

Table 2:
ID1 ParentID2
ID2 ParentID2
ID3 ParentId2

So when the rows are joined we have:

ParentID 2& Id1 only, not ID2 and ID3.

How do I do that?

I want to check the null value of the Id1 column to see if there is a record in that table, but I definitely don't want my parent row to show up 3 times, just the once.

thanks.
0
Comment
Question by:Starr Duskk
[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
  • 3
  • 3
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38793922
Please view his article 8806085321953
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 400 total points
ID: 38793929
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 38794032
that doesn't help.

A DISTINCT won't work because the value of the joined table's primary ID field is going to be unique.

I also don't see how a group by is going to resolve this.

Do you or anyone have any specific solutions to my problem?

thanks.
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38794040
the "ROWNUMBER()" method will work, and that is exactly what the article is about :)
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 38794050
I feel I'm on a wild goose chase. I don't find any code in there using a JOIN and the snippet they have for mssql server doesn't show a row number. So unless you can dredge out of that article a snippet for me that works, I'm not closing this.
0
 
LVL 27

Accepted Solution

by:
Chris Luttrell earned 1600 total points
ID: 38794135
This is what andelIII is trying to show you in his article, you end up with something like this where you use the ROW_NUMBER() function to assign a rank (the row_number) per grouping (the Partition) to your secondary table so that when you join to it, you can match on just the first value.
SELECT * -- Pick the columns you want to see here
FROM dbo.Table1 T
LEFT OUTER JOIN (SELECT *, ROW_NUMBER() OVER (PARTITION BY ParentID ORDER BY ID) rn
				FROM dbo.Table2 T2) T2 ON T.ParentID = T2.ParentID AND rn = 1

Open in new window

You get results like this (I put an ID of 1 with no children to show it works both with and without child records)
Results
0
 
LVL 2

Author Closing Comment

by:Starr Duskk
ID: 38794553
Thanks for all the additional effort!
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

636 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