• derek.colley (9/5/2012)


    Assuming a structure, at its simplest, as:

    CREATE TABLE table1 (id INT, a INT)

    CREATE TABLE table2 (id INT, b INT)

    INSERT INTO table1

    SELECT 1,1 UNION SELECT 2,2 UNION SELECT 3,3

    INSERT INTO table2

    SELECT 1,1 UNION SELECT 2,2 UNION SELECT 3,3

    Then:

    CREATE TABLE table3 (id INT IDENTITY(1,1), e INT, f INT,

    divided AS ((e/f) * CAST(1 AS DECIMAL(10,2)) ) )

    INSERT INTO table3 (e, f)

    SELECT (SUM(t1.a) / COUNT(t1.id)) [e],

    (SUM(t2.b) / COUNT(t2.id)) [f]

    FROM table1 t1, table2 t2

    The 'divided' column in table3 will be your result. You can use temporary tables instead of permanent ones for simplicity.

    Tested OK.

    Derek,

    Try it with the test data I posted and see how long it takes because of the CROSS JOIN and see that it still only comes up with one row even with your data.

    Also, the OP wanted the difference between the two averages.

    --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)