Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

Add one subquery to existing query to get the percentage from another table

$
0
0
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

Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>