Backward recursive query using CTE

Hi I have one table called cllquestion. in that i have got question which have parent question.

I cna create the hirearcy using CTE  like child questions for a parent question. But i want it backwards like i have child question  and i want all ancestor question for it.

Could you pelase give me the code that how i can do this.

Regards
Nitesha
RedshelfAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anilallewarCommented:
0
ThomasianCommented:
You just need to invert the fields when joining the table to the cte.
DECLARE @t table (id int, parent int)

INSERT INTO @t VALUES (1,NULL)
INSERT INTO @t VALUES (2,1)
INSERT INTO @t VALUES (3,1)
INSERT INTO @t VALUES (4,2)
INSERT INTO @t VALUES (5,3)
INSERT INTO @t VALUES (6,4)

--To get the children of a given parentid
DECLARE @parentid int
SET @parentid = 2
;WITH CTE (id, parent, level) AS
(
SELECT id, parent, 0 FROM @t WHERE id=@parentid 
UNION ALL
SELECT T1.id, T1.parent, level + 1
FROM @t T1 INNER JOIN CTE T2 ON T1.parent = T2.id 
)
SELECT * FROM CTE


--To get the ancestors of a given childid
DECLARE @childid int
SET @childid = 6
;WITH CTE (id, parent, level) AS
(
SELECT id, parent, 0 FROM @t WHERE id=@childid 
UNION ALL
SELECT T1.id, T1.parent, level + 1
FROM @t T1 INNER JOIN CTE T2 ON T1.id = T2.parent
)
SELECT * FROM CTE

Open in new window

0
RedshelfAuthor Commented:
Please check the folllowing code.

if i  use this procedure then i can get all t he child question for the question id(@id).
bu i want the other way round. like i want all parent questioins for the @ID .

if table is
id parentquestionid
1 0
2 1
3 1
4 2
5 2
6 3
7 3
8 5
9 8

so @id=5 then result shoul be
5 2
2 1
1 0

following code gives me all the child question so id @id=5 then result is.
5 2
8 5
9 8

 any help will be appreciated.

N
ALTER PROCEDURE [dbo].[sp_getparentlist] (@id int)

AS

           ;WITH parentlist(id,text,parentquestionid,lastquestion,priority,level,Depth)

            AS

            (

              SELECT id,text,parentquestionid,lastquestion,priority,level,0 
              FROM callquestions
where id=@id
              Union All
              SELECT E.ID, E.text,e.parentquestionid, E.lastquestion, E.priority, E.level, M.Depth-1
                 
            FROM callquestions AS E
                        JOIN parentlist AS M
                          ON E.parentquestionid = M.id 
            )

            SELECT         id,text,parentquestionid,lastquestion,priority,level,Depth

            FROM parentlist

Open in new window

0
ThomasianCommented:
Try
           ;WITH parentlist(id,text,parentquestionid,lastquestion,priority,level,Depth)

            AS

            (

              SELECT id,text,parentquestionid,lastquestion,priority,level,0 
              FROM callquestions
where id=@id
              Union All
              SELECT E.ID, E.text,e.parentquestionid, E.lastquestion, E.priority, E.level, M.Depth-1
                 
            FROM callquestions AS E
                        JOIN parentlist AS M
                          ON E.id = M.parentquestionid
            )

            SELECT         id,text,parentquestionid,lastquestion,priority,level,Depth

            FROM parentlist

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RedshelfAuthor Commented:
thank you Thomasian .

It works fine now.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.