I am using MsSQL and I want to add some values where some conditions are met. Is it possible to use WHERE clause in the sum() function? Or is there an equivalent function to the excel SUMIF().
2 Answers
The SUM aggregate will only operate on the result set constrained by the WHERE clause.
In other words, yes, this is fine.
SELECT SUM(days)
FROM myTable
WHERE something = another
You could use CASE:
SELECT SUM(CASE WHEN YourCondition=1 THEN 1 ELSE 0 END)
FROM YourTable
1 Comment
bendataclear
+1 over Oded's method, this allows you to group the results which (I guess) would be more useful to the OP.