Click here to Skip to main content
15,911,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
declare @nodeid int = '1';
with cte as ( 
select  cust_ID, name,null lnode, null rnode from  user_detail where 
cust_ID = @nodeid 

union all select t.cust_ID,t.name, ISNULL(cte.lnode, CASE WHEN t.joinside = 
0 THEN 1 ELSE 0 END) 
lnode, ISNULL(cte.rnode, CASE WHEN t.joinside = 1 THEN 1 ELSE 0 END) 
rnode from  user_detail t inner join cte on cte.cust_ID = t.parentid )
select cust_ID,name from cte where rnode='0' option (maxrecursion 0)


Current Scenario:

cust_id name
1 Harry
5 Ravi
7 Ram
9 Tommy
10 Romi

What i want is:

cust_id name status(installments table)
1 Harry paid
5 Ravi not paid
7 Ram not paid
9 Tommy paid
10 Romi paid

now i will explain what i want: the above query is getting results only from user_detail table. now i want to modify the query in such a way that it will also search in "installments" table for the "status" column of the respective cust_id (which the above query is returning).

The modified query will get the value of status from installments table based on cust_id.

And query will show the results in the third column as showing in 2nd screenshot.

i am not much familiar with cte and nested queries. I hope you guys will understand my problem.

What I have tried:

i am not familiar with cte and nested sub queries
Posted
Updated 23-Dec-18 19:39pm
v2

1 solution

select t.cust_ID,t.name, (CASE ISNULL(cte.lnode, 0) WHEN 1 THEN 'paid' ELSE 'not paid' END) lnode, (CASE ISNULL(cte.rnode, 0) WHEN 1 THEN 'paid' ELSE 'not paid' END)
rnode 
from  user_detail t 
      left join cte on cte.cust_ID = t.parentid 
where user_detail.cust_ID = 'YOUR_ID'
 
Share this answer
 
Comments
Harpreet_125 24-Dec-18 5:06am    
giving error... please write full query

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900