dido
dido

Reputation: 3407

How can I get a difference of two columns in two different tables in a SELECT query

I have the two queries below I'd like to combine into one and get a difference result.

Query 1

SELECT SUM(col1 + col2) As total FROM tableA

Query 2

SELECT SUM(total) FROM tableB WHERE color not like '%black' and model not like 'CF%'

I'd like to combine these in a SELECT query and get the result: Query 1 - Query 2 = result. The tables both have a "id" as a common key between them. I'm using MS SQL Server 2008

Upvotes: 0

Views: 1754

Answers (2)

Quassnoi
Quassnoi

Reputation: 425371

SELECT  (
        SELECT  SUM(col1 + col2)
        FROM    tableA
        ) -
        (
        SELECT  SUM(total) 
        FROM    tableB
        WHERE   color NOT LIKE '%black'
                AND model NOT LIKE 'CF%'
        ) AS result

Upvotes: 2

Kris.Mitchell
Kris.Mitchell

Reputation: 998

Do an outside select with both of your queries as columns.

Something like

SELECT (Query1) - (Query2) as Diff

Upvotes: 1

Related Questions