我无法按照我想要的顺序从数据库中获取值
I am not able to get the values from the database in the order I want
我正在尝试从数据库中获取 android 中条形图的值。
id amount status month
1 3567 + 4
2 5673 - 3
3 2356 + 2
4 789 - 8
5 1789 + 8
请考虑这个 table。 (对不起,我无法添加图像)。因为,我需要获取条形图的值,所以我希望在 x 轴上显示月份,在 y 轴上显示数量。条形图应该是收入与支出图表。
我应用了以下查询:
Cursor c=db1.rawQuery("select distinct month, (select sum(amount) from expenses where status like '+' group by month) as income, (select sum(amount) from expenses where status like '-' group by month)as expense from expenses", null);
我得到以下结果:
month income expense
4 2356 5673
3 2356 5673
2 2356 5673
8 2356 5673
而不是
month income expense
4 3567 null
3 null 5673
2 2356 null
8 1789 789
我希望如果有更多的行(比如说,month=4 和 status="+", amount=20),那么结果应该是 3587 而不是 3567。
感谢您的宝贵时间和帮助。
select distinct month,
(select sum(amount) from expenses where month = t.month and status like '+' group by month) as income,
(select sum(amount) from expenses where month = t.month and status like '-' group by month) as expense
from expenses t
如果您执行子查询,您需要加入数据,因为子查询将搜索所有 table(s)。
对不起,我没看到只有 1 个 table 也试试这个。
select
distinct month,
(select sum(ammount) from foo as f2 where f1.month=f2.month and status='+' ) as income,
(select sum(ammount) from foo as f3 where f1.month=f3.month and status='-') as expense
from foo f1
我正在尝试从数据库中获取 android 中条形图的值。
id amount status month
1 3567 + 4
2 5673 - 3
3 2356 + 2
4 789 - 8
5 1789 + 8
请考虑这个 table。 (对不起,我无法添加图像)。因为,我需要获取条形图的值,所以我希望在 x 轴上显示月份,在 y 轴上显示数量。条形图应该是收入与支出图表。
我应用了以下查询:
Cursor c=db1.rawQuery("select distinct month, (select sum(amount) from expenses where status like '+' group by month) as income, (select sum(amount) from expenses where status like '-' group by month)as expense from expenses", null);
我得到以下结果:
month income expense
4 2356 5673
3 2356 5673
2 2356 5673
8 2356 5673
而不是
month income expense
4 3567 null
3 null 5673
2 2356 null
8 1789 789
我希望如果有更多的行(比如说,month=4 和 status="+", amount=20),那么结果应该是 3587 而不是 3567。
感谢您的宝贵时间和帮助。
select distinct month,
(select sum(amount) from expenses where month = t.month and status like '+' group by month) as income,
(select sum(amount) from expenses where month = t.month and status like '-' group by month) as expense
from expenses t
如果您执行子查询,您需要加入数据,因为子查询将搜索所有 table(s)。 对不起,我没看到只有 1 个 table 也试试这个。
select
distinct month,
(select sum(ammount) from foo as f2 where f1.month=f2.month and status='+' ) as income,
(select sum(ammount) from foo as f3 where f1.month=f3.month and status='-') as expense
from foo f1