我有一个数据集,如果每个组中的一个子组都满足特定条件,我将在该数据集上尝试为每个组求和。我不确定这是否可行,或者我是否错误地解决了此问题。
我的数据结构如下:
+----+-------------+---------+-------+
| ID | Transaction | Product | Value |
+----+-------------+---------+-------+
| 1 | A | 0 | 10 |
| 1 | A | 1 | 15 |
| 1 | A | 2 | 20 |
| 1 | B | 1 | 5 |
| 1 | B | 2 | 10 |
+----+-------------+---------+-------+
在此示例中ID column
,如果交易不包含任何标记为0的产品,我想通过来获得值的总和。在上述情况下,与关联的所有值都Transaction A
将被排除,因为Product 0
已购买了该值。结果是:
+----+-------------+
| ID | Sum of Value|
+----+-------------+
| 1 | 15 |
+----+-------------+
IDs
如果交易不包含乘积0,则此过程将重复多个,每个ID仅包含值的总和。
嗯。。。一种方法是not exists
用于过滤:
select id, sum(value)
from t
where not exists (select 1
from t t2
where t2.id = t.id and t2.transaction = t.transaction and
t2.product = 0
)
group by id;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句