0

我有这样的 MyTbl

Id  type    amount
--  ----    ------
1   1       100
2   1       200
3   2       300
4   2       400

是否可以在 Android 中编写一个返回这个的 sqlite 查询?

Amount1 Amount2
------- -------
300     700

我已经编写了以下查询,但是不行:

select SUM(a.amount) as Amount1, SUM(b.amount) as Amount2 from MyTbl a inner join MyTbl b
on a.id = b.id
group by a.type

它返回:

Amount1 Amount2
------- -------
300     300
700     700
4

1 回答 1

2
SELECT
    SUM(CASE WHEN type = 1 THEN amount ELSE 0 END) amount1,
    SUM(CASE WHEN type = 2 THEN amount ELSE 0 END) amount2
FROM MyTbl

SQL 小提琴

于 2013-01-19T07:28:11.093 回答