简体   繁体   中英

SQLite Select different where clause

I'm stuck with queries. Basically I want to get two results of the same field, according to two where clauses, then. Sum (field1) when (ins_n = 0) and sum (field1) when (ins_n = 1). I hope I explained. Now I can only do so when (ins_n = 0)

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

You can use SUM and CASE :

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

If you need result in two rows use @Marc B suggestion:

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

SqlFiddleDemo2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM