Home > DeveloperSection > Forums > sql subtract subquery sum multiple tables
Andrew Deniel
Andrew Deniel

Total Post:29

Points:203
Posted on    September-28-2013 8:03 AM

 MSSQL Server MSSQL Server 
Ratings:


 1 Reply(s)
 2070  View(s)
Rate this:

How can I subtract the sum of a subquery table from another table?

SELECT i.column1 * i.column2 AS Expr1
    , i.column1 * i.column2 - (SELECT SUM(table2.column1) AS Expr1 
                               FROM table2 
                               WHERE (table2.column3 = table1.column3)) AS derivedExpression
FROM table1

Only the derivedExpression in the first row is correct. The rest rows returns null for derivedExpression. For Expr1, everything is fine. Any help?



Ida B Wells
Ida B Wells

Total Post:30

Points:210
Posted on    September-28-2013 8:58 AM


SELECT
i.column1 * i.column2 AS Expr1
    , i.column1 * i.column2 - (SELECT COALESCE(SUM(table2.column1), 0) AS Expr1 
                               FROM table2 
                               WHERE (table2.column3 = table1.column3)) AS derivedExpression
FROM table1

Don't want to miss updates? Please click the below button!

Follow MindStick