Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MS SQL join complex subquery

Posted on 2013-05-13
2
Medium Priority
?
429 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 71

Accepted Solution

by:
Qlemo earned 2000 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

704 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