Click here to Skip to main content
16,020,261 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
actually i am working on a project in which members are added in a tree pattern, and get the payment accordingly.

My tbltestingtree structure is ass follow:
Id         ParentId        IsLeft           IsRight
 1          Null             Null             Null
 2           1                1               Null
 3           1               Null               1
 4           2                1               Null
 5           2               Null               1
 6           3                1               Null
 7           3               Null               1
 8           4                1               Null
 9           4               Null               1
 10          5                1               Null

**the problem is that initially 1500$ are given to parent when two nodes are added to its left and one to his right(2:1) . and then 500$ for each pair.**

My problem is to find the query which can return the total income of any given node.

http://demosite2.netbridgeindia.com/imagesashu/payment1.jpg[^]

According to figure node 1 must get 2500$ (1500+500+500) first 500$ is for node 4 and second 500$ is for node 3.

According to figure node 2 must get 1500$ because it has two nodes to its left and one node to its right this means a ratio of (2:1). and has no pairs

According to figure node 3 must get 0$ because it does not have any nodes in ratio(2:1)

one thing has to be kept in mind that 1500$ will be the first payment and then only the other pairs will be counted, and 1500$ will be given when node has ratio 2:1(two nodes on left and one on right) but no money when ratio is 1:2(one node on left and two on right)


I have found the query which will count all the pairs below a particular node and give receiving amount according to 500$, but the query has not been able to consider the first condition that is the 2:1 condition

SQL
      declare @ParentId as int
       set @ParentId=1

     create table #temp_table_name
      (
        ParentId varchar(30) null,
       )


    ;with Child as
     (
  select id,ParentId from tblTestingTree where id=@ParentId
  union all
  Select tblTestingTree.Id,tblTestingTree.parentId from tblTestingTree
  inner join Child
  on tblTestingTree.ParentId=Child.Id
     )

insert into #temp_table_name
select c.ParentId  from tblTestingTree T join Child c
on c.Id=t.Id
WHERE ISNULL(T.ParentId, 0) <> 0 and c.ParentId!=@ParentId
group by c.ParentId
having COUNT(c.ParentId)>1

select COUNT(*)*500 as totalmoney from #temp_table_name

drop table #temp_table_name


Please Help
Posted
Updated 4-Oct-13 20:10pm
v3
Comments
ashumeerut 7-Oct-13 4:15am    
I need Help I can't Figure Out This Problem

1 solution

Have a look at your diagram ;)

There are 3 rings:
- black (nodes: 1, 2, 3, 4),
- blue (nodes: 4, 8, 9),
- blue (nodes: 3, 6, 7).

Node 1 has 3 subnodes with relationship 2:1
- 2 (4 and 5) and 4 (8 and 9) on its left
- 3 (6 and 7) on its right
but only subnode no. 2 has its pair (2 on left and 1 on right).
Subnode: 3th and 4th does not have their pairs (2 on left and 1 on right).

Conclusion... There is something wrong with your logic, because you take subnodes 3th and 4th, but they have 1:1 relationship (1 node on the left and 1 node on the right side).

If i understand you well, only subnode no. 2 should be add to sum ;)
 
Share this answer
 
Comments
ashumeerut 24-Nov-13 23:00pm    
Sir,Thanks for your time, I want to tell you that my logic (conditions) are correct, actually the problem is that initially 1500$ are given to parent when two nodes are added to its left and one to his right(2:1) . and then 500$ for each pair

when node 2,4 are added to the left of node 1 and node 3 is added to its right then node 1 gets 1500$ (2:1) and after that when node 2 gets paired then node 1 gets 500$ more. and when node 3 gets paired then node 1 gets 500$ more so total amount for node 1 is(1500+500+500)=2500
Maciej Los 25-Nov-13 2:06am    
Please, share full code. Use "Improve question" widget to upgrade your question.

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