sql如何根据区分条件查询集获取不同值的总和

时间:2015-12-06 07:44:06

标签: mysql sql

我想获得两列,其中group1具有不同的值,其中c3 = yes和group2,其中它根据group1具有sum(c2)。

c1   |   c2   | c3
--------------|-----
A    |   2    |yes
B    |   1    |yes
A    |   2    |yes
B    |   1    |yes
C    |   2    |no
C    |   1    |no




result :


group1 | group2
---------------
A      | 4
B      | 2

我正在尝试此查询但不能正常工作

SELECT SUM(c2) WHERE DISTINCT c1 in('A','B') and c3='yes' as 'group2' from table,
    SELECT DISTINCT c1 in('A','B') as 'group1' from table

1 个答案:

答案 0 :(得分:2)

SELECT c1 as group1, SUM(c2) as group2 FROM table 
WHERE c3="yes"
GROUP BY c1