Solved

MS SQL join complex subquery

Posted on 2013-05-13
2
405 Views
Last Modified: 2013-08-05
I am trying to combine my query into a join to bring back some records.

Here is the first part of my query, this does exactly what I want and returns the latest record from this result set as each one is linked to the other as show below.

pkSDSID   fkSDSID
 50605     NULL
 88377     50605
 90602     88377
 90616     90602

This query returns 90616 from the results.

DECLARE @LatestSDS INT

with tree as (
   SELECT pkSDSID, fkSDSID
   FROM tblSDS
   WHERE pkSDSID = 50605

   UNION ALL

   SELECT t1.pkSDSID, t1.fkSDSID
   FROM tblSDS t1
   JOIN tree p ON p.pkSDSID = t1.fkSDSID
)

SELECT TOP 1 @LatestSDS = pkSDSID FROM tree ORDER BY pkSDSID DESC

SELECT @LatestSDS

This is fine if I have one record I want to find but what I'm now stuck on his how to do this for multiple records.

I was wondering if I could inner join this somehow using it as a sub query of my main query but I cant seem to find a way to get it to work.

What I would like is to use the pkSDSID like above which is 50605 but use a column of a table and get the LastestSDS for each record.

I hope this makes sense I'm not all that great with SQL and I just don't know where to go from here.

Is this possible?

I appreciate all help. Thank you.
0
Comment
Question by:Fezi
2 Comments
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39161990
Is it safe to assume the keys are in ascending order for a tree? In that case
with tree as
(
   SELECT pkSDSID as StartSID, pkSDSID, fkSDSID
   FROM tblSDS
   WHERE fkSDSID is NULL

   UNION ALL

   SELECT p.StartSID, t1.pkSDSID, t1.fkSDSID
   FROM tblSDS t1
   JOIN tree p ON p.pkSDSID = t1.fkSDSID
)
select StartSID, max(pkSDSID) from tree group by StartSID

Open in new window

should work.
0
 

Author Closing Comment

by:Fezi
ID: 39382220
Perfect
0

Featured Post

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.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

808 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