Creating SumIf function in SQL Server 2012

I need some help building a SQL Server function that acts as a SumIf in Excel, for example

SumIF(Fees.Fee_Amount, Fees.Type ='Services' and Fees.Fee_Code = 'B01') 

so the item that would be summed if it is a Fees.Fee_Amount and the where part is Fees.Type ='Services' and Fees.Fee_Code = 'B01'

Syntax would be SumIf(TableName.ColumnName, Criteria), the function would return the total.

1 Answer

The simplest way would be to SUM a CASE clause, like so:

SUM(CASE WHEN Fees.Type ='Services' and Fees.Fee_Code = 'B01' THEN Fees.Fee_Amount END) AS ColumnAlias, 
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like