How to find the total number of pair under particular parent according to pattern 1:2or2:1,1:1 day to day maximum up to 5 pair caping daily

Dear friends,
I provide full details here ,my database table structure ,data and stored procedure and my problem ,
please review it and provide the solution or any idea to solve my problem.
I am working on a project in which members are added in a tree pattern, and get the payment accordingly.
below is my table structure ,data and stored procedure
CREATE TABLE Associate_Income
ID varchar(30) NOT NULL,
ParentID varchar(30) NULL,
IsLeft tinyint NULL,
IsRight tinyint NULL,
joingdate datetime NOT NULL
go
INSERT Associate_Income
(ID, ParentID, IsLeft, IsRight, joingdate)
SELECT 'Ramesh123', NULL, NULL, NULL '2014-01-03 16:31:15.000' UNION ALL
SELECT 'Sonu', 'Ramesh123', 1, NULL, '2014-01-03 16:45:21.000' UNION ALL
SELECT 'Pawan kumar', 'Ramesh123', NULL, 1, '2014-01-04 16:50:23.000' UNION ALL
SELECT 'Ravi123', 'Sonu', 1, NULL, '2014-01-04 17:03:22.000' UNION ALL
SELECT 'Vineet123', 'Sonu', NULL, 1, '2014-01-04 17:26:01.000' UNION ALL
SELECT 'dev123', 'Ravi123', 1, NULL, '2014-01-05 19:35:16.000' UNION ALL
SELECT 'Mukesh123', 'Ravi123', NULL, 1, '2014-01-05 19:40:41.000' UNION ALL
SELECT 'poonam123', 'Vineet123', 1, NULL, '2014-01-05 19:49:49.000' UNION ALL
SELECT 'monu', 'Pawan kumar', 1, NULL, '2014-01-05 17:32:58.000' UNION ALL
SELECT 'Arti123', 'Pawan kumar', NULL, 1, '2014-01-05 19:54:35.000' UNION ALL
My  database table Associate_Income  structure and data is as follow:
ID ParentID IsLeft IsRight joingdate
Ramesh123 NULL NULL NULL 2014-01-03 16:31:15.000
Sonu Ramesh123 1 NULL 2014-01-03 16:45:21.000
Pawan kumar Ramesh123 NULL 1 2014-01-04 16:50:23.000
Ravi123 Sonu 1 NULL 2014-01-04 17:03:22.000
Vineet123 Sonu NULL 1 2014-01-04 17:26:01.000
dev123 Ravi123 1 NULL 2014-01-05 19:35:16.000
Mukesh123 Ravi123 NULL 1 2014-01-05 19:40:41.000
poonam123 Vineet123 1 NULL 2014-01-05 19:49:49.000
monu Pawan kumar 1 NULL 2014-01-05 17:32:58.000
Arti123 Pawan kumar NULL 1 2014-01-05 19:54:35.000
by using below stored procedure i can count the total number of pairs under particular node in 2:1,1:1 ratio means first pair is completed when two node added to the left side of given parent node and one node added
right side of given parent node after that all pairs are completed when one node added left side and one node added right side of parent node (1:1 ratio)
example if i execute my stored procedure as follows it would return following.
EXEC count_pairs 'Ramesh123'
3
so there is 3 pairs as shown in my figure.
when we execute my stored procedure for ParentID 'sonu' it would return following.
EXEC count_pairs 'sonu'
2
so there is 2 pairs as shown in my figure.
My problem is to find the query which can return the total number of pair under particular node any given parent  node. day to
day maximum 5 pairs in a day please any one can suggest us
CREATE proc [dbo].[count_pairs]
@ParentID nvarchar(50)
as
begin
Declare @ParentSUM SMALLINT = 0
Declare @SubLeftID nvarchar(50)
Declare @SubRightID nvarchar(50)
SELECT @SubLeftID = CASE WHEN [IsLeft] = 1 THEN [ID] ELSE @SubLeftID END
,@SubRightID = CASE WHEN [IsRight] = 1 THEN [ID] ELSE @SubRightID END
FROM Associate_Income
WHERE ParentID = @ParentID
IF @SubLeftID IS NOT NULL AND @SubRightID IS NOT NULL AND EXISTS(SELECT 1 FROM Associate_Income WHERE [IsLeft] = 1 AND ParentID = @SubLeftID)
BEGIN
SET @ParentSUM = 1
;WITH Associate_Income_CTE AS
SELECT [ID], [ParentID], [IsLeft], [IsRight], 0 AS [Level]
FROM Associate_Income
WHERE [ParentID] = @ParentID
UNION ALL
SELECT RecursiveMember.[ID], RecursiveMember.[ParentID], RecursiveMember.[IsLeft], RecursiveMember.[IsRight], Level + 1
FROM Associate_Income RecursiveMember
INNER JOIN Associate_Income_CTE AnchorMember
ON RecursiveMember.[ParentID] = AnchorMember.[ID]
SELECT @ParentSUM = @ParentSUM + COUNT([ParentID])
FROM
SELECT [ParentID]
,'IsLeft' AS [Direction]
,1 AS [Value]
FROM Associate_Income
WHERE [IsLeft] = 1
AND [ID] <> @ParentID --AND [ID] NOT IN (@SubLeftID, @ParentID)
AND [ParentID] NOT IN (@ParentID, @SubLeftID)
UNION ALL
SELECT [ParentID]
,'IsRight' AS [Direction]
,1 AS [Value]
FROM Associate_Income
WHERE [IsRight] = 1
AND [ParentID] <> @ParentID
) AS Associate_Income
PIVOT
MAX([Value]) FOR [Direction] IN ([IsLeft], [IsRight])
) PVT
WHERE [IsLeft] IS NOT NULL AND [IsRight] IS NOT NULL
END
SELECT @ParentSUM
Jitendra Kumar Sr. Software Developer at Ruvixo Technologies 7895253402

I don't think this is homework, I am not sure how helpful it was by Kalman to merge the two threads. It appears that two different persons posted the questions. It could be though, that it is the same problem and Jitendra has taken over Chandra's
task.
However, I was not able to understand the problem nor the figure. And nor the definition of pairs in this context. My assumption is that what Jitendra posted is an abstraction of the actual problem in order to not reveal intellecutal property. Possibly this
makes the problem more difficult to understand for us outsiders.
I've come so far that I worked out a table definition and INSERT statements with sample data, as well as a call to the procedure which returns 3 and this appears to map to the figure, but I don't know what it means. Since I don't know what this
is about, I have not made any attepmts to understand the code, but I would appreciate clarification about the underlying business rules as well as the expected results for various test cases.
CREATE TABLE Associate_Income(ID varchar(30) NOT NULL,
ParentID varchar(30) NULL,
IsLeft tinyint NULL,
IsRight tinyint NULL,
joingdate datetime NOT NULL)
go
INSERT Associate_Income (ID, ParentID, IsLeft, IsRight, joingdate)
SELECT 'Ramesh123', NULL, NULL, NULL, '2014-01-03 16:31:15.000' UNION ALL
SELECT 'Sonu', 'Ramesh123', 1, NULL, '2014-01-03 16:45:21.000' UNION ALL
SELECT 'Pawan kumar', 'Ramesh123', NULL, 1, '2014-01-04 16:50:23.000' UNION ALL
SELECT 'Ravi123', 'Sonu', 1, NULL, '2014-01-04 17:03:22.000' UNION ALL
SELECT 'Vineet123', 'Sonu', NULL, 1, '2014-01-04 17:26:01.000' UNION ALL
SELECT 'dev123', 'Ravi123', 1, NULL, '2014-01-05 19:35:16.000' UNION ALL
SELECT 'Mukesh123', 'Ravi123', NULL, 1, '2014-01-05 19:40:41.000' UNION ALL
SELECT 'poonam123', 'Vineet123', 1, NULL, '2014-01-05 19:49:49.000' UNION ALL
SELECT 'monu', 'Pawan kumar', 1, NULL, '2014-01-05 17:32:58.000' UNION ALL
SELECT 'Arti123', 'Pawan kumar', NULL, 1, '2014-01-05 19:54:35.000'
go
CREATE proc [dbo].[count_pairs]
@ParentID nvarchar(50)
as
begin
Declare @ParentSUM SMALLINT = 0
Declare @SubLeftID nvarchar(50)
Declare @SubRightID nvarchar(50)
SELECT @SubLeftID = CASE WHEN [IsLeft] = 1 THEN [ID] ELSE @SubLeftID END
,@SubRightID = CASE WHEN [IsRight] = 1 THEN [ID] ELSE @SubRightID END
FROM Associate_Income
WHERE ParentID = @ParentID
IF @SubLeftID IS NOT NULL AND @SubRightID IS NOT NULL AND EXISTS(SELECT 1 FROM Associate_Income WHERE [IsLeft] = 1 AND ParentID = @SubLeftID)
BEGIN
SET @ParentSUM = 1
;WITH Associate_Income_CTE AS
SELECT [ID], [ParentID], [IsLeft], [IsRight], 0 AS [Level]
FROM Associate_Income
WHERE [ParentID] = @ParentID
UNION ALL
SELECT RecursiveMember.[ID], RecursiveMember.[ParentID], RecursiveMember.[IsLeft], RecursiveMember.[IsRight], Level + 1
FROM Associate_Income RecursiveMember
INNER JOIN Associate_Income_CTE AnchorMember
ON RecursiveMember.[ParentID] = AnchorMember.[ID]
SELECT @ParentSUM = @ParentSUM + COUNT([ParentID])
FROM
SELECT [ParentID]
,'IsLeft' AS [Direction]
,1 AS [Value]
FROM Associate_Income
WHERE [IsLeft] = 1
AND [ID] <> @ParentID --AND [ID] NOT IN (@SubLeftID, @ParentID)
AND [ParentID] NOT IN (@ParentID, @SubLeftID)
UNION ALL
SELECT [ParentID]
,'IsRight' AS [Direction]
,1 AS [Value]
FROM Associate_Income
WHERE [IsRight] = 1
AND [ParentID] <> @ParentID
) AS Associate_Income
PIVOT
MAX([Value]) FOR [Direction] IN ([IsLeft], [IsRight])
) PVT
WHERE [IsLeft] IS NOT NULL AND [IsRight] IS NOT NULL
END
SELECT @ParentSUM
END
go
EXEC count_pairs 'Ramesh123'
go
DROP PROCEDURE count_pairs
DROP TABLE Associate_Income
Erland Sommarskog, SQL Server MVP, [email protected]

Similar Messages

  • How do I find the total number of elements in a multi dim array

    How do I find the total number of elements in a single or multi dim array?
    For example, a 2x3 array has 6 elements, and a 2x3x4 has 24. How do I compute this very easily - is there a single VI that does this?
    David
    Solved!
    Go to Solution.

    Use "array size" (array palette) followed by "multiply array elements" (numeric palette)
    (Works also equally well for 3D arrays and higher)
    For a 1D array, just use "array size".
    Message Edited by altenbach on 02-05-2009 05:57 PM
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    NumberOfElements.png ‏3 KB

  • How do i find the total number of fields within all tables in one of my databases

    I'm in the process of creating some "gee whiz" metrics for one of my applications and want to find the total number of columns contained in its database.  Is there a stored procedure to get this information that would save me the effort
    of opening each of my 66 tables and counting the columns?  I also have the same question for my 138 views.  Thank you for any ideas you care to offer...............Phil Hoop
    Phil Hoop

    Hi Phil,
    Assumes SQL 2005 or higher
    Let 's try simple one ....
    SELECT COUNT(col.column_name), col.table_name
    FROM information_schema.columns col
    JOIN information_schema.tables tbl
    ON tbl.table_name = col.table_name
    AND tbl.table_schema = col.table_schema
    AND tbl.table_catalog = col.table_catalog
    AND tbl.table_type = 'VIEW'
    GROUP BY col.table_name
    Let me know if this will help you.
    If you think my suggestion is useful, please rate it as helpful.
    If it has helped you to resolve the problem, please Mark it as Answer.
    Varinder Sandhu www.varindersandhu.in

  • How to find the total no of pages in script

    hi
    how to find the total no of pages in script

    Hi
    ·     &SAPSCRIPT-FORMPAGES&:
    This field contains a number representing the total number of pages of the currently formatted form (any output between START_FORM and END_FORM). The page counter mode (START, INC, HOLD) of the individual pages is ignored. You can use this symbol to formulate information like
    ‘Page x of y’ for your output.
    ·     &SAPSCRIPT-JOBPAGES&:
    This field contains a number representing the total number of pages of all forms contained in the currently formatted print request, in other words, of all forms created using the OPEN_FORM, START_FORM.. ENDFORM, START_FORM.. END_FORM,..., CLOSE_FORM function modules.
    When using the SAPSCRIPT-FORMPAGES or SAPSCRIPT-JOBPAGES symbols, SAPscript leads all output pages of the current form or current print request into main memory to replace the symbol by the appropriate value. For large output jobs, this can mean a very large amount of memory.
    ·     &PAGE&, &NEXTPAGE&
    This symbols are initially converted using the options specified in the form of the page definition.
    Reward all helpfull answers
    Regards
    Pavan

  • How to get the total number of pages printed in a report?

    Hi All,
    I have a requirement where I need to print a frame of fields only in the last page. Unfortunately I cannot use the 'Print Object On' property as it doesnt work in my case. So, I am planning to write a format trigger on the frame to return TRUE if the page is the last physical page. Now, I need to know how to get the total number of physical pages that will get printed in the report so that I can use this to manipulate the frame. I was planning to use the 'Total Physical Pages' built-in, but it seems like I can just use it to print in a field and I can't use this field's value anywhere in the plsql code (formula column function/format trigger) in the report. Is there anyway to get the total number of pages printed in the report which can be used in the report plsql code?
    Thanks,
    Srini.

    i found the solution, thanks

  • How to find the total no of users in a client

    Hi Techie's,
    How to find the total no. of users in the client ? is it possible ?
    i have tried out with the steps by viewing the table USR01,but i can't get the actual solution ?
    solutions rewarded!
    regards,
    S.Rajeshkumar

    Hi rajesh,
    You can use SU10, then click on "Authorization data" -> put * on user -> execute.. you should get a list of all users in the client with details included...
    Also you can list them using USMM and check all licensed users....
    Also SUIM -> users by complex selection criteria -> By user id -> put * on user -> execute.
    Regards
    Juan

  • Find the total number of saved reports in Disco 10g using Disco Admin tool

    Hi,
    I am trying to find the total number of saved or scheduled reports in Discoverer 10g using Discoverer Administrator tool. Can anyone help?
    Irene.

    The EUL STATUS workbooks might help
    http://docs.oracle.com/html/B13916_04/eul_status.htm
    HTH
    Srini

  • Someone stole my 4th gen. Ipod touch and i don't know how to find the serial number... Can you track it?

    I dont know how to find the serial number and someone stole my i pod can someone track it?

    You can easily get the serial number.  Having it will not help you track it.
    iPod: How to find the serial number
    If you activated find my ipod on the ipod BEFORE it went missng, then you may be able to track it.
    Otherwise, you cannot track it.

  • HT2452 How to find the serial number of your Apple hardware product

    How to find the serial number of your Apple hardware product

    This works for any Mac provided that the logic board has not been replaced.
    I recently had the logic board replaced in my iMac. After the tech left (on site repair thanks to Applecare), I went to about the Mac and found jibberish numbers. I called Apple and the AASP and found that he had forgotten to register the serial number. So he came back and, with a special tool (as he called it - looked a little like a USB stick) he contacted Apple and then entered my original serial number as I read it to him.
    So, I assume they changed the process since I still have the same serial number as before.

  • TS1702 how to find the serial number, after purchase Pages online?

    how to find the serial number, after purchase Pages online?

    If you downloaded the standalone trial version of Lightroom, it will be necessary for you to uninstall that version. Then you will be able to install Lightroom through the creative cloud application manager. After you install Lightroom that way, just double-click on the catalog that you have already started.

  • How to find the CRM related Tables form a Particular DataSource

    Hi friends
               <b>How to find the CRM related Tables form a Particular DataSource
      of BusinessContent.</b>
      ex: i had taken CRM - DataSource ........ 0CRM_LEAD_H (Lead Header Data)
           for this DS i assign IS and extract data. But i want to Know from which
           CRM tables it was comming.
           ie ..I want to know the what tables linked to Datasource(0CRM_LEAD_H)
           How it has to check?

    Just for your info, you can find as useful (for a methodology to solve this kind of issue) this weblog:
    SAP BW and Business Content Datasources: in pursuit of the origins
    Hope it helps!
    Bye,
    Roberto

  • How to find the serial number for more than one Ipod Touch in Itunes?

    I lost my Ipod Touch (first one I bought), how can I find the serial number in Itunes or somewhere else beside I sync my daughter's new Ipod (2nd I bought) with my Itunes account, because when I try to find the serial number they show me only my daughters Ipod, not my Ipod????

    Hover the mouse pointer over your iPod backup in iTunes>Preferences>Devices and the SN will pop up.
    Go to:
    My Support Profile

  • HT1349 In the recent version Itunes, I can't find the total number of songs status line in my song library window as before.

    Downloaded the most recent versin of Itunes. Now I can't see the total number of songs in my Itunes library as before. It used to be on a status line at bottom of song Library screen. Where can I view the Total ???

    From the View menu > Select  "Show Status bar...'
    http://osxdaily.com/2012/11/29/5-tips-make-itunes-look-normal/

  • How to find the total no of Deliveries per sales order?

    Hi,
    I am writing a custom development specification.
    Is there a SAP table/field by which I can know the total number of deliveries created for a single sales order ?
    For example, if a Sales order is created for quantity 10 and we have two delivers ( each of 5) ..is there a field I can get that information from?
    --Sidd

    Thanks for the reply.
    I am not sure if VBFA will work...I would like the program to look into all deliveries for a sales order and make a distinction between the first one & the rest.
    For example, if the sales order has 5 deliveries associated with it.
    I would like to run a specific logic for the first delivery & a different logic for the rest of the deliveries.
    Is there a way to tell the system ...to distinguish between the different deliveries ?
    -Sidd
    Edited by: sbhattacharya99 on Apr 15, 2010 11:50 PM

  • How to find the serial number

    I cant find the serial number on the back of the dvd case? Which one is that?

    justperso
    What version of Premiere Elements do you have and what computer system is involved?
    I have Premiere Elements in the box packaging for versions 4 through 12, so once you supply the information requested, I will give you exact details on where to find the serial number.
    Here is a link to give you the general idea
    https://helpx.adobe.com/x-productkb/global/find-serial-number.html
    ATR

Maybe you are looking for