简体   繁体   English

仅当组内的每个值都大于0时,SQL SUM

[英]SQL SUM only when each value within a group is greater than 0

Here is a sample data set: 这是一个示例数据集:

ID   Value
1   421
1   532
1   642
2   3413
2   0
2   5323

I want a query that, in this case, only sums ID=1 because all of its values are greater than 0. I cannot use a WHERE statement that says WHERE Value > 0 because then ID=2 would still return a value. 我想要一个查询,在这种情况下,由于所有值都大于0,因此只求和ID = 1。我不能使用WHERE语句表示WHERE Value > 0因为那时ID = 2仍会返回值。 I feel like this may be an instance where I could possibly use a OVER(PARTITION BY...) statement, but I am not familiar enough to use it creatively. 我觉得这可能是我可以使用OVER(PARTITION BY...)语句的一个实例,但是我对创建它的用途并不熟悉。

As an aside, I don't simply add a WHERE ID = 1 statement because this needs to cover a much larger data set. 顺便说一句,我不只是添加WHERE ID = 1语句,因为这需要覆盖更大的数据集。

Just use having : 只需使用having

select id, sum(value)
from t
group by id
having min(value) > 0;

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

相关问题 仅当值大于 0 时才对分区求和 - Sum over Partition By Only when Value is Greater than 0 仅当值等于或大于 3,500 时求和? - SUM when a value is equal or greater than 3,500 only? 仅当sum大于值时才返回行 - Only return rows if sum is greater than a value SQL - 只显示大于 0 的值 - SQL - with only showing a value greater than 0 SQL仅在总和大于100时才选择所有记录 - SQL select all records only if the sum is greater than 100 SQL仅在总和大于0时选择所有记录 - SQL select all records only if the sum is greater than 0 获得下一个最小值,大于或等于每个组的给定值 - Get next minimum, greater than or equal to a given value for each group SQL:分组和计数,但仅限于所有记录都大于阈值 - SQL: group and count but only if ALL records are greater than a threshold SQL 对于 A 列中的每个唯一值,对 B 列求和。如果总和大于 50,则创建列 C 并添加值“Y”,否则添加“N” - SQL For each unique value in Column A, sum Column B. If sum is greater than 50, create Column C and add value “Y”, else “N” mysql 查询每个组的 select 最大值,如果最大值大于该组的所有其他值 - mysql query to select max value of each group if the maximum value is greater than all the other values of the group
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM