Creating a SumIf Function in SQL Server 2012

I need help creating a SQL Server function that acts like SumIfin Excel, for example

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

therefore, the element that will be summed if it is equal Fees.Fee_Amount, and where - Fees.Type ='Services'andFees.Fee_Code = 'B01'

The syntax will be SumIf(TableName.ColumnName, Criteria), the function will return the total number.

+4
source share
1 answer

The simplest way would be a SUM CASE clause, for example:

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

All Articles