Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am working on multi level marketing website binary tree based.Below query is returning me total number of left side and right side members. Below query is working fine. Now i want to add one more condition in the below query. That basically will be the sub query. i am not familiar with the sub queries or nested queries. so i want help from you guys.

cmd = new SqlCommand("declare @nodeid int = '"+cust_id+ "';with cte as (select cust_id, parentid, joinside, null left_node, null right_node from  user_detail where cust_id = @nodeid union all select t.cust_id, t.parentid, t.joinside,ISNULL(cte.left_node, CASE WHEN t.joinside = 0 THEN 1 ELSE 0 END) left_node,ISNULL(cte.right_node, CASE WHEN t.joinside = 1 THEN 1 ELSE 0 END) right_node from  user_detail t inner join cte on cte.cust_id = t.parentid ) select @nodeid nodeid, SUM(left_node) LeftNodes,SUM(right_node) RightNmodes from cte option (maxrecursion 0)", con);


What i want is: i have another table installments in which there is a flag column which contains values "paid" and "not paid". i want to count %age of left side and right side customers who have paid the installments of specific id. i want this condition in the above query. i hope you guys understand my question. Thanks

What I have tried:

i have not tried anything yet.. i am not familiar with nested or sub queries.
Posted
Updated 4-Jan-19 8:34am
Comments
Wendelius 23-Dec-18 12:27pm    
Could yo post the table definitions for all the tables participating in the query. That would help to understand the situation
Maciej Los 24-Dec-18 3:31am    
Note, that your sql command is sql injection vulnerable!

1 solution

Without seeing your table structure, I can only guess what you want.

A simple subquery for what you want would be included in your WHERE clause and might look like this:

WHERE ...<existing clause>...
AND cte.InstallmentId in (Select InstallmentId from Installments where flag = 'paid')

Now that subquery can be as simple or as complex as you need it to be, but it should return a list of a single column that you can query with an "IN" clause.

If you need multiple columns, you can also JOIN to a subquery.

In your table section you can do

FROM ...<existing from clause>...
LEFT OUTER JOIN (Select InstallmentId, Age from Installments where flag = 'paid') as I on cte.InstallmentID = I.InstallmentID and cte.Age = I.Age

Hopefully that is enough to get you going.

Brent
 
Share this answer
 
v2

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