Home Forums SQL Server 2008 SQL Server Newbies How to get a sum of values in a table joind to table of sum values RE: How to get a sum of values in a table joind to table of sum values

  • Here's a code sample on how to do it:

    WITH cteTable1 AS(
        SELECT col1, col2, SUM(col3) sumcol3, SUM(col4) sumcol4
        FROM Table1
        GROUP BY col1, col2
    ),
    cteTable2 AS(
        SELECT col1, col2, SUM(col3) sumcol3, SUM(col4) sumcol4
        FROM Table2
        GROUP BY col1, col2
    )
    SELECT  *
    FROM Table1 AS t1
    JOIN Table2 AS t2 ON t1.col1 = t2.col1;

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2