473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to find the total number of pair under particular parent according to pattern 1:2

My tbltestingtree structure is as follow:
Expand|Select|Wrap|Line Numbers
  1.      ID            ParentID       IsLeft  IsRight             joingdate
  2.      Ramesh123       NULL           NULL       NULL              2014-01-03 16:31:15.000
  3.      Sonu            Ramesh123      1          NULL              2014-01-03 16:45:21.000
  4.      Pawan kumar     Ramesh123      NULL       1                 2014-01-04 16:50:23.000
  5.      Ravi123         Sonu           1          NULL              2014-01-04 17:03:22.000
  6.      Vineet123       Sonu           NULL       1                 2014-01-04 17:26:01.000
  7.      dev123          Ravi123        1          NULL              2014-01-05 19:35:16.000
  8.      Mukesh123       Ravi123        NULL       1                 2014-01-05 19:40:41.000
  9.      poonam123       Vineet123      1          NULL              2014-01-05 19:49:49.000 
  10.      monu            Pawan kumar    1          NULL              2014-01-05 17:32:58.000
  11.      Arti123         Pawan kumar    NULL       1                 2014-01-05 19:54:35.000        
  12.  
  13.  
My problem is to find the query which can return the total number of pair under particular node any given node. day by day

Expand|Select|Wrap|Line Numbers
  1. CREATE proc [dbo].[count_pairs]
  2. (
  3. @ParentID nvarchar(50)
  4.  
  5. )
  6. as
  7. begin
  8. Declare @ParentSUM SMALLINT = 0
  9. Declare @SubLeftID nvarchar(50)
  10. Declare @SubRightID nvarchar(50)
  11. SELECT @SubLeftID = CASE WHEN [IsLeft] = 1 THEN [ID] ELSE @SubLeftID END
  12.       ,@SubRightID = CASE WHEN [IsRight] = 1 THEN [ID] ELSE @SubRightID END
  13. FROM  Associate_Income
  14. WHERE ParentID = @ParentID
  15.  
  16.  
  17. IF @SubLeftID IS NOT NULL AND @SubRightID IS NOT NULL AND EXISTS(SELECT 1 FROM  Associate_Income WHERE [IsLeft] = 1 AND ParentID = @SubLeftID)
  18. BEGIN
  19.  
  20.     SET @ParentSUM = 1
  21.  
  22.     ;WITH  Associate_Income_CTE AS
  23.     (
  24.  
  25.         SELECT [ID], [ParentID], [IsLeft], [IsRight], 0 AS [Level] 
  26.         FROM Associate_Income
  27.         WHERE [ParentID] = @ParentID
  28.         UNION ALL
  29.         SELECT RecursiveMember.[ID], RecursiveMember.[ParentID], RecursiveMember.[IsLeft], RecursiveMember.[IsRight], Level + 1 
  30.         FROM Associate_Income RecursiveMember
  31.         INNER JOIN  Associate_Income_CTE AnchorMember
  32.             ON RecursiveMember.[ParentID] = AnchorMember.[ID]
  33.     )
  34.     SELECT @ParentSUM = @ParentSUM + COUNT([ParentID]) 
  35.     FROM
  36.     (
  37.         SELECT [ParentID]
  38.               ,'IsLeft' AS [Direction]
  39.               ,1 AS [Value]
  40.         FROM  Associate_Income
  41.         WHERE [IsLeft] = 1
  42.             AND [ID] <> @ParentID --AND [ID] NOT IN (@SubLeftID, @ParentID)
  43.             AND [ParentID] NOT IN (@ParentID, @SubLeftID)
  44.         UNION ALL
  45.         SELECT [ParentID]
  46.               ,'IsRight' AS [Direction]
  47.               ,1 AS [Value]
  48.         FROM  Associate_Income
  49.         WHERE [IsRight] = 1
  50.             AND [ParentID] <> @ParentID
  51.     ) AS  Associate_Income
  52.     PIVOT
  53.     (
  54.         MAX([Value]) FOR [Direction] IN ([IsLeft], [IsRight])
  55.     ) PVT
  56.     WHERE [IsLeft] IS NOT NULL AND [IsRight] IS NOT NULL
  57.  
  58. END
  59.  
  60. SELECT @ParentSUM
  61.  
  62.  
above is my stored procedure by which we can count total number of pairs under particular parent according to a particular pattern 1:2or2:1,1:1

please any one suggest us how can count day to day total number of pairs under particular node maximum up to 5 pair capping daily
Attached Images
File Type: jpg ss.jpg (40.1 KB, 129 views)
Jan 6 '14 #1
1 1301
Rabbit
12,516 Expert Mod 8TB
Your question is difficult to understand because you leave so many of your terms undefined.

What I can say is that your table structure is not good for storing hierarchical data. Instead, you should use a modified preorder tree traversal algorithm. You can read the details of said structure here: http://www.sitepoint.com/hierarchical-data-database-2/
Jan 6 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: John | last post by:
hey all..... alright, I am frusterated to the point of throwing my machine out the window (this board went down, trying to find stuff on google, this has been a nightmare) so I hope you guys can...
2
by: msnews | last post by:
Hi All, We have a requirement to display total number of current users using the site. We want to display only the total no other information. And this value needs to change dynamically. Is it...
7
by: Egor Shipovalov | last post by:
I'm implementing paging through search results using cursors. Is there a better way to know total number of rows under a cursor than running a separate COUNT(*) query? I think PostgreSQL is bound...
4
by: simonmarkjones | last post by:
How can i find out record number and total number of records and display in a text box? Want to to this as I dont want to use the standard access navigation buttons.
2
by: patang | last post by:
I found the following to count the total number of VISIBLE rows of datagrid datagrid.visiblerowcount How do we count the total number of ACTUAL (not just visible) rows of datagrid? Thanks...
4
by: MA | last post by:
Hi, How to access the total number of child nodes from a parent node. For example, I would like to get the total number of child nodes from <parent1and <parent2node. The SelectNodes method...
1
by: meryline | last post by:
hi, iam new to c#, can any one tell me how to find the total number characters entered in a textbox. like in vb.net i find it using len(textbox.text) . iam trying to implement the same in c# sharp...
5
by: gnewsgroup | last post by:
In my user control, I would like to find a Label control in the parent page (the page that uses my user control). I need to update that Label.Text when something happens in the user control. I...
2
by: APEJMAN | last post by:
HI I am trying to find out the total number of recoreds in a Hash Table. I am trying to write a function to calculate the total number of the recored in the Hash Table here is what I figure out,...
2
by: alwaali | last post by:
Hi I need help please This is my project and i need a help to solve it with you A page of text is to be read and analyzed to determine number of occurrences and locations of different words. The...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.