1

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().

1
  • 3
    Post your query and we can help. Commented May 16, 2012 at 15:36

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

sorry I didn't explain but I don't to use the condition in the where clause
@Mhretabk - Well, where are you using a condition then? Posting the query in your question would have left no doubt.
1

You could use CASE:

SELECT SUM(CASE WHEN YourCondition=1 THEN 1 ELSE 0 END)
FROM YourTable

1 Comment

+1 over Oded's method, this allows you to group the results which (I guess) would be more useful to the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.