Solved

MS SQL join complex subquery

Posted on 2013-05-13
2
423 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
[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
2 Comments
 
LVL 70

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

736 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