SQLite选择不同的where子句

时间:2015-12-09 18:46:43

标签: sql sqlite select

我遇到了疑问。基本上我想得到两个相同字段的结果,根据两个where子句,然后。 当(ins_n = 0)时的Sum(field1)和当(ins_n = 1)时的sum(field1)。我希望我解释一下。现在我只能在(ins_n = 0)

时这样做
"SELECT " +
"SUM(field1) AS incasso1, " +                
"FROM Movimentinegozio WHERE ins_n = 0 ";

1 个答案:

答案 0 :(得分:1)

您可以使用SUMCASE

SELECT SUM(CASE WHEN ins_n = 0 THEN field1 END) AS incasso0,
       SUM(CASE WHEN ins_n = 1 THEN field1 END) AS incasso1
FROM Movimentinegozio
WHERE ins_n IN (0, 1);

SqlFiddleDemo

如果您需要两行结果,请使用@Marc B建议:

SELECT ins_n, SUM(field1) AS incasso
FROM Movimentinegozio
WHERE ins_n IN (0, 1) 
GROUP BY ins_n

SqlFiddleDemo2