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 开发者_Python百科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
SELECT SUM(A) AS SumA,
SUM(B) AS SumB,
SUM(CASE WHEN B IS NOT NULL THEN A END) As Foo, /*... Rest of Query*/
精彩评论