QuakerOat
QuakerOat

Reputation: 1143

SQL - constraints which effect only one of the values returned?

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

I have a table with columns A and B amongst others. I would like to obtain the sum for A and the sum for B (according to the WHERE clause I state), along with various other values. However, it is possible for B to contain null values. I would also like, returned via the same query, the value of the sum of A's for which the corresponding B value is not null. This is the only value returned which I want to be effected by whether or not B is null.

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

Any help is much appreciated

Upvotes: 4

Views: 46

Answers (1)

Martin Smith
Martin Smith

Reputation: 452977

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

Upvotes: 4

Related Questions