Restrictions that affect only one of the return values?

I would like to generate an SQL query to achieve the following:

I have a table with columns A and B among others. I would like to get the sum for A and the sum for B (according to the WHERE clause I), as well as various other values. However, B may contain null values. I would also like to return, on the same query, the value of the sum A, for which the corresponding value of B is not null. This is the only return value I want to execute, regardless of whether B is null.

So, any suggestions on how I can achieve this? I work in SQL Server 2008.

Any help is much appreciated

+4
source share
1 answer
SELECT SUM(A) AS SumA, SUM(B) AS SumB, SUM(CASE WHEN B IS NOT NULL THEN A END) As Foo, /*... Rest of Query*/ 
+4
source

All Articles