Groups | Blog | Home
all groups > sql server (microsoft) > june 2005 >

sql server (microsoft) : subtract rows


pikapi
6/21/2005 12:40:01 AM
Hi,
I have the following table:

INOUT Total
1 3
2 2
2 4
1 3
2 6
1 23
2 33

I want to make a query that will do the following:

Subtract (the sum of the total where inout=1) from (the sum of the
total where inout=2)

Any help is greatly appreciated. Thanks
David Portas
6/21/2005 3:59:02 AM
SELECT
SUM(
CASE inout
WHEN 1 THEN -total
WHEN 2 THEN total
END)
FROM YourTable

--
David Portas
SQL Server MVP
--
pikapi
6/21/2005 7:59:23 AM
Thanks David.
It works
AddThis Social Bookmark Button