Solved

MS SQL join complex subquery

Posted on 2013-05-13
2
397 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 68

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

943 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

10 Experts available now in Live!

Get 1:1 Help Now