在 SQL Server 2012 中创建 SumIf 函数 [英] Creating SumIf function in SQL Server 2012
本文介绍了在 SQL Server 2012 中创建 SumIf 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一些帮助来构建在 Excel 中充当 SumIf
的 SQL Server 函数,例如
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')
因此,如果它是 Fees.Fee_Amount
并且其中部分是 Fees.Type ='Services'
和 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'
语法为 SumIf(TableName.ColumnName, Criteria)
,函数将返回总数.
Syntax would be SumIf(TableName.ColumnName, Criteria)
, the function would return the total.
推荐答案
最简单的方法是 SUM 一个 CASE 子句,如下所示:
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,
这篇关于在 SQL Server 2012 中创建 SumIf 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文