Solved

MS SQL join complex subquery

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Creating and Managing Databases with phpMyAdmin in cPanel.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

746 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

12 Experts available now in Live!

Get 1:1 Help Now