Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Recursive Query Expand / Collapse
Author
Message
Posted Friday, October 15, 2010 5:07 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, March 4, 2015 3:56 AM
Points: 8, Visits: 32
I've a table with data shown as below

DetailsID FollowupID
1 0
2 0
3 1
4 2
5 3

if you look at the above data, "3" is a followup of 1 and "5" is a followup of "3"

Similarly "4" is a followup of "2"

and 1 and 2 are original ids or parent ids.

I've to write a query that will retrieve only the parent and the last child of that parent.

I used recursion but that retrieves the entire chain. How do I further get only the parent and the last child
Post #1005053
Posted Friday, October 15, 2010 6:24 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 10:00 AM
Points: 8,296, Visits: 17,994
Post what you've tried already.
Can you set up some sample data? One of the links in my sig will show you how to do this.


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1005093
Posted Friday, October 15, 2010 6:54 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Monday, January 11, 2016 11:54 AM
Points: 517, Visits: 1,785
you can do this;

;
WITH
cteFindMax AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY DetailsID, FollowupID ORDER BY FollowupID DESC) AS Occurance,
*
FROM [the name of your table]
)
SELECT *
FROM cteFindMax
WHERE Occurance = 1

Post #1005114
Posted Sunday, October 24, 2010 7:50 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 8:15 AM
Points: 40,984, Visits: 38,281
sridharkannan7 (10/15/2010)
I've a table with data shown as below

DetailsID FollowupID
1 0
2 0
3 1
4 2
5 3

if you look at the above data, "3" is a followup of 1 and "5" is a followup of "3"

Similarly "4" is a followup of "2"

and 1 and 2 are original ids or parent ids.

I've to write a query that will retrieve only the parent and the last child of that parent.

I used recursion but that retrieves the entire chain. How do I further get only the parent and the last child


Considering the LOS that some folks gave you on this, allow me to ask... did you ever get an answer for this that worked for you?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

Helpful Links:
How to post code problems
How to post performance problems
Post #1009763
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse