• AhmetEmre90 (2/20/2013)


    Hi;

    I have two SQL tables like below;

    T1: relationID, meterID, parentID, childID

    T2: dataID, meterID, date, amount

    Sample data of tables;

    T1 T2

    ----------------- -------------------------

    1 | 1 | null | 2 * 1 | 1 | 01,01,2013 | 100 *

    2 | 1 | null | 3 * 2 | 2 | 01,01,2013 | 60 *

    3 | 2 | 1 | 4 3 | 3 | 01,01,2013 | 40 *

    4 | 2 | 1 | 5 4 | 4 | 01,01,2013 | 35

    5 | 3 | 1 | 6 5 | 5 | 01,01,2013 | 25

    6 | 3 | 1 | 7 6 | 6 | 01,01,2013 | 15

    7 | 4 | 2 | null 7 | 7 | 01,01,2013 | 25

    8 | 5 | 2 | null

    9 | 6 | 3 | null

    10 | 7 | 3 | null

    I want to compare if the sum of the children's amount is equal to parent's amount.

    For example; meter1 is parent of meter2 and meter3 (lines with *). I want to check if 100 = 60 + 40.

    How can i do this with SQL query.

    You can see the relation between the meters at the image below.

    Sorry for my poor English.

    I believe that the following article will give you what you want and much more.

    http://www.sqlservercentral.com/articles/T-SQL/94570/

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)