Sql server Getting all questions under a technology and its child (n-level)

I am having a technology table where I am having data as follows

    TechId Desc ParentId
       1    A     Null
       2    B      1
       3    C      1
       4    D      2

Open in new window


My Question table is as follows

    QId     TId      desc
     1       1        Q1
     2       2        Q2
     3       3        Q3
     4       4        Q4 

Open in new window


What I am trying to get is from questions table is when I select TechId 1 it should load all questions up to nth level. Result expecting is

QId TId Desc
 1      1      Q1
  2     2       Q2
  3     3        Q3
  4     4        Q4

Open in new window

Chaitanya VAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Pawan KumarDatabase ExpertCommented:
Please try complete solution -

CREATE TABLE technology
(
	 TechId INT
	,[Desc] VARCHAR(1)
	,ParentId INT 
)
GO
    
INSERT INTO technology VALUES
(1,'A',Null),
(2,'B', 1  ),
(3,'C', 1  ),
(4,'D', 2  )
GO

CREATE TABLE Question
(
     QId  INT   
	,TId INT 
	,[desc] VARCHAR(10)
)
GO

INSERT INTO Question VALUES
(1, 1,'Q1'),
(2, 2,'Q2'),
(3, 3,'Q3'),
(4, 4,'Q4')
GO

DECLARE @techID AS INT 
;WITH CTE AS
(
	SELECT Q.*,ParentID FROM technology t 
	INNER JOIN Question Q ON Q.TId = t.TechId
)
,CTE1 AS
(
	SELECT * FROM CTE WHERE TId = @techID
)
,CTE2 AS 
(
	SELECT a.QId,a.TId,a.[desc] FROM CTE a
	UNION ALL  
	SELECT t.QId,t.TId,t.[desc]
	FROM CTE2 c INNER JOIN CTE t ON c.TId = t.ParentID
)
SELECT DISTINCT * FROM CTE2

/*------------------------
OUTPUT
------------------------*/
QId         TId         desc
----------- ----------- ----------
1           1           Q1
2           2           Q2
3           3           Q3
4           4           Q4

(4 row(s) affected)

Open in new window


OUTPUT

/*------------------------
OUTPUT
------------------------*/
QId         TId         desc
----------- ----------- ----------
1           1           Q1
2           2           Q2
3           3           Q3
4           4           Q4

(4 row(s) affected)

Open in new window

Chaitanya VAuthor Commented:
Hi one small help can I get the count like for TechnologyId 1 it should show as 4 and for 2 as 1 respectively
Pawan KumarDatabase ExpertCommented:
Updated code for you -

DECLARE @techID AS INT = 1
;WITH CTE AS
(
	SELECT Q.*,ParentID FROM technology t 
	INNER JOIN Question Q ON Q.TId = t.TechId
)
,CTE1 AS
(
	SELECT * FROM CTE WHERE TId = @techID
)
,CTE2 AS 
(
	SELECT a.QId,a.TId,a.[desc] FROM CTE1 a
	UNION ALL  
	SELECT t.QId,t.TId,t.[desc]
	FROM CTE2 c INNER JOIN CTE t ON c.TId = t.ParentID
)
SELECT DISTINCT * , COUNT(*) OVER() Cnt FROM CTE2

Open in new window


Output

/*------------------------
OUTPUT
------------------------*/
QId         TId         desc       Cnt
----------- ----------- ---------- -----------
1           1           Q1         4
2           2           Q2         4
3           3           Q3         4
4           4           Q4         4

(4 row(s) affected)

Open in new window

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
Pawan KumarDatabase ExpertCommented:
Provided verified solution.
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.