Solved

Left Outer Join with only 1 result

Posted on 2013-01-18
7
347 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
  • 3
  • 3
7 Comments
 
LVL 142

Expert Comment

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

Assisted Solution

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

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 142

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 1

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 26

Accepted Solution

by:
Chris Luttrell earned 400 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 1

Author Closing Comment

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

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax using Declare 4 38
In or Between 2 44
SQL Server stored proc 2 13
Anyway to make these 2 SQL statements into one? 13 27
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

861 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now