在几个条件下计算总数 [英] Count total on couple of conditions
本文介绍了在几个条件下计算总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一张这样的表:
Suppose I have a table like this:
id col1 col2 col3
1 a 1 k1
2 a 2 k2
3 a -3 k3
4 b 3 k4
现在,我想要'col1'与'id' = 1行中'col1'相同的所有行的'col2'列的总数.
Now, I want the total of column 'col2' for all rows where 'col1' is the same as 'col1' in the row where 'id' = 1.
所以这就是 1+2-3=0.
So that would be 1+2-3=0.
我将如何用 SQL 表达这一点?
How would I express this in SQL?
推荐答案
SELECT SUM(col2) from TABLE
where col1 in (SELECT col1 from TABLE where id = 1)
这篇关于在几个条件下计算总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文