简体   繁体   English

带有where子句的SQL sum函数

[英]SQL sum function with where clause

I am using MsSQL and I want to add some values where some conditions are met. 我正在使用MsSQL,我想在满足某些条件的情况下添加一些值。 Is it possible to use WHERE clause in the sum() function? 是否可以在sum()函数中使用WHERE子句? Or is there an equivalent function to the excel SUMIF(). 或者是excel SUMIF()的等效函数。

The SUM aggregate will only operate on the result set constrained by the WHERE clause. SUM聚合仅对WHERE子句约束的结果集进行操作。

In other words, yes, this is fine. 换句话说,是的,这很好。

SELECT SUM(days)
FROM myTable
WHERE something = another

You could use CASE : 你可以使用CASE

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM