---
title: "sql subtract subquery sum multiple tables"  
description: "sql subtract subquery sum multiple tables"  
author: "Andrew Deniel"  
published: 2013-09-28  
updated: 2013-09-28  
canonical: https://www.mindstick.com/forum/1563/sql-subtract-subquery-sum-multiple-tables  
category: "mssql server"  
tags: ["mssql server"]  
reading_time: 1 minute  

---

# sql subtract subquery sum multiple tables

How can I subtract the [sum](https://www.mindstick.com/forum/159332/how-can-i-use-sum-in-linq) of a [subquery](https://www.mindstick.com/forum/1279/using-alias-in-subquery) [table from another table](https://www.mindstick.com/forum/190/create-a-new-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](https://www.mindstick.com/forum/1212/this-row-already-belongs-to-this-table) is correct. The [rest](https://www.mindstick.com/forum/12745/how-do-i-use-webapi-rest-correctly-when-other-params-are-needed) rows [returns null](https://www.mindstick.com/forum/159370/sql-join-returns-null-records-when-there-is-no-data-in-the-database-in-sql-server-why) for derivedExpression. For Expr1, everything is fine. Any help?

## Replies

### Reply by Anonymous User

```
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 derivedExpressionFROM table1
```

\

\


---

Original Source: https://www.mindstick.com/forum/1563/sql-subtract-subquery-sum-multiple-tables

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
