Syntax issue in my SQL Query

My query post the fee to the Parent Doc based on number of child doc. But i need to skip the billed child which has been already billed.

Expected output from my code snippet would be

Parent Doc      BilledChildDoc      Fee      IsChildBecameParent

AB12456       NX12450              0.4      FALSE
CY12345        NX23410                      0      TRUE
CY12345        NX23421                      0.4      FALSE
CY12345        NX23432                      0.4      FALSE
NX23410       NY23411                     0.4      FALSE
NX23410          NY23422                 0      TRUE
NY23422         NZ23411                   0.4      FALSE
NY23422       NZ23422              0.4      FALSE

Create table Sample ( OriginalDoc VARCHAR(255), ChildDoc VARCHAR(255), Fee MONEY );

VALUES  ( 'AB12456', 'NX12450', NULL ),
    ( 'CY12345', 'NX23410', NULL ),             
    ( 'CY12345', 'NX23421', NULL ),             
    ( 'CY12345', 'NX23432', NULL ),             
    ( 'NX23410', 'NY23411', NULL ),             
    ( 'NX23410', 'NY23422', NULL ),             
    ( 'NY23422', 'NZ23411', NULL ),             
    ( 'NY23422', 'NZ23422',NULL);

with cte as(select *, OriginalDoc as Parent from Sample s1 
            where not exists(select * from Sample s2 where s2.ChildDoc = s1.OriginalDoc)
            union all
            select s.*, c.Parent from cte c
            join Sample s on c.ChildDoc = s.OriginalDoc
select Parent, count(*)*0.4 as Fee from cte
group by Parent

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsackerContract ERP Admin/ConsultantCommented:
I made a working solution using a temporary table:

    OriginalDoc         VARCHAR(255),
    ChildDoc            VARCHAR(255),
    Fee                 MONEY,
    IsChildBecameParent VARCHAR(5) );

VALUES  ( 'AB12456', 'NX12450', NULL, NULL ),
        ( 'CY12345', 'NX23410', NULL, NULL ),             
        ( 'CY12345', 'NX23421', NULL, NULL ),             
        ( 'CY12345', 'NX23432', NULL, NULL ),             
        ( 'NX23410', 'NY23411', NULL, NULL ),             
        ( 'NX23410', 'NY23422', NULL, NULL ),             
        ( 'NY23422', 'NZ23411', NULL, NULL ),             
        ( 'NY23422', 'NZ23422', NULL, NULL );

UPDATE  @Sample
SET     Fee = 0,
        IsChildBecameParent = 'TRUE'
WHERE   ChildDoc IN (SELECT OriginalDoc FROM @Sample)

UPDATE  @Sample
SET     Fee = 0.4,
        IsChildBecameParent = 'FALSE'
WHERE   ChildDoc NOT IN (SELECT OriginalDoc FROM @Sample)


Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chokkaStudentAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.