如何显示每个/选定月份的总费用?

问题描述 投票:-1回答:1

我想在日历下方显示每个/选定月份的总费用。

我将费用总额存储在数据库中,如下所示:

  public double getTotal(String email, int month) {
    SQLiteDatabase db = this.getWritableDatabase();
    int total;

    Cursor cursor = db.rawQuery("SELECT SUM( Amount ) as Amount FROM " + "UserExpenses" +  " WHERE " + KEY_MONTH + "=" + month + "",null);

    if (cursor.moveToFirst()) {

         total = cursor.getInt(cursor.getColumnIndex("Amount"));// get final total
         return total;
    }
    else
        total = -1;
    cursor.close();
    return 0;
}

即使我在滚动月份,我也只能获得所选月份的总费用

 Calendar c = Calendar.getInstance();
        int month = c.get(Calendar.MONTH);

        totalExpense = dataBaseHelper.getTotal(email, month);

        expense = findViewById(R.id.expense);
        expense.setText(String.valueOf(totalExpense));
android sqlite android-sqlite
1个回答
2
投票

您可以对使用GROUP BY子句分组的数据运行使用sum(x)函数的查询。

  • 其中x表示列名。

sum(x)函数是一个聚合函数,它返回添加列x的结果,它将作用于GROUP BY子句确定的一组行。

GROUP BY子句应根据年份和月份进行分组。

因此,SQL就是这样的

SELECT  sum(your_amount_column), your_year_column, your_month_column FROM your_table GROUP BY your_email_column,your_year_column, your_month_column;

您希望按日历下方的每个/选定月份进行选择。那么WHERE子句也应该允许选择。通过将日期存储为单独的实体(即,年月和日是单独的列),选择变得复杂。因此,为了选择一系列日期(假设存储jan 1,对于feb 2存储等),则出现问题20192(feb 2019)大于201912(如果简单地连接则排序),因此需要一些操作,例如将年份乘以100并加上。

因此,为了选择日期范围,使用BETWEEN子句的WHERE子句可以使用以下内容: -

WHERE (year * 100) + month BETWEEN ((start_year * 100) + start_month) AND ((end_year * 100) + end_month)
  • 以上假设适用于所有电子邮件帐户(每个电子邮件帐户将有单独的行/总和)。对于单个电子邮件帐户,将添加WHERE子句。

SQL Example

考虑以下 :-

DROP TABLE IF EXISTS user_balance;
CREATE TABLE IF NOT EXISTS  user_balance (email TEXT, amount REAL, description TEXT, year INTEGER, month INTEGER, day INTEGER, created_at TEXT);
INSERT INTO user_balance VALUES
    ('Fred',5.50,'blah',2019,01,01,'2019-01-01 10:30'),
    ('Fred',10.50,'blah',2019,01,02,'2019-01-01 10:30'),
    ('Fred',15.50,'blah',2019,01,13,'2019-01-01 10:30'),
    ('Fred',20.50,'blah',2019,02,04,'2019-01-01 10:30'),
    ('Fred',25.50,'blah',2019,02,05,'2019-01-01 10:30'),

    ('Mary',145.25,'blah',2019,01,01,'2019-01-01 10:30'),
    ('Mary',145.25,'blah',2019,01,02,'2019-01-01 10:30'),
    ('Mary',145.25,'blah',2019,01,13,'2019-01-01 10:30'),
    ('Mary',145.25,'blah',2019,02,04,'2019-01-01 10:30'),
    ('Mary',145.25,'blah',2019,02,05,'2019-01-01 10:30'),

    ('Joan',345.25,'blah',2019,01,01,'2019-01-01 10:30'),
    ('Joan',345.25,'blah',2019,01,02,'2019-01-01 10:30'),
    ('Joan',345.25,'blah',2019,01,13,'2019-01-01 10:30'),
    ('Joan',345.25,'blah',2019,02,04,'2019-01-01 10:30'),
    ('Joan',345.25,'blah',2019,02,05,'2019-01-01 10:30'),

    ('Fred',45.25,'blah',2018,01,01,'2019-01-01 10:30'),
    ('Fred',45.25,'blah',2018,01,02,'2019-01-01 10:30'),
    ('Fred',45.25,'blah',2018,01,13,'2019-01-01 10:30'),
    ('Fred',45.25,'blah',2018,02,04,'2019-01-01 10:30'),
    ('Fred',45.25,'blah',2018,02,05,'2019-01-01 10:30'),

    ('Mary',145.25,'blah',2018,01,01,'2019-01-01 10:30'),
    ('Mary',145.25,'blah',2018,01,02,'2019-01-01 10:30'),
    ('Mary',145.25,'blah',2018,01,13,'2019-01-01 10:30'),
    ('Mary',145.25,'blah',2018,02,04,'2019-01-01 10:30'),
    ('Mary',145.25,'blah',2018,02,05,'2019-01-01 10:30'),

    ('Joan',345.25,'blah',2018,01,01,'2019-01-01 10:30'),
    ('Joan',345.25,'blah',2018,01,02,'2019-01-01 10:30'),
    ('Joan',345.25,'blah',2018,01,13,'2019-01-01 10:30'),
    ('Joan',345.25,'blah',2018,02,04,'2019-01-01 10:30'),
    ('Joan',345.25,'blah',2018,02,05,'2019-01-01 10:30')
;
SELECT email, sum(amount) AS balance, year, month 
    FROM user_balance 
    WHERE 
        ((year * 100) + month) BETWEEN ((2018 * 100) + 09) AND ((2019 * 100) + 09)  
    GROUP BY email,year,month ;
SELECT email, sum(amount) AS balance, year, month 
    FROM user_balance 
    WHERE 
        ((year * 100) + month) BETWEEN ((2018 * 100) + 09) AND ((2019 * 100) + 09)  
        and email = 'Mary' 
    GROUP BY email,year,month;

这个

  1. DROPs和CREATE用户平衡表(为了方便运行/测试/更改)示例。
  2. 为3个电子邮件/用户添加一些测试数据,每个用户2年的数据为2个月。
  3. 为2018年9月(2018 09) - 2019年9月(2019 09)范围内的所有电子邮件/用户选择每月收入(金额)的总和。
  4. 如上所述,但对于特定的电子邮件/用户。

两个结果是: -

enter image description here

enter image description here

4 Android (Java)

为了与Android一起使用并使用SQLite数据库qazxsw poi方法返回一个Cursor,你可以使用以下内容:

query
  • 注意以上是原则上的代码,它尚未经过测试或运行,因此可能包含错误。
  • 考虑到以下情况,您可能希望将此方法命名为更具描述性,例如:也许public Cursor getUserBalance(String email, int start_year, int start_month, int end_year, int end_month) { String[] columns = new String[]{ KEY_EMAIL, "sum(" + KEY_AMOUTNT + ") AS balance", KEY_YEAR, KEY_MONTH }; String whereclause = "((year * 100) + month) BETWEEN ((? * 100) + ?) AND ((? * 100) + ?) and email = ?"; String[] whereargs = new String[]{ String.valueOf(start_year), String.valueOf(start_month), String.valueOf(end_year), String.valueOf(end_month), email }; String groupbyclause = KEY_EMAIL + "," + KEY_YEAR + "," + KEY_MONTH; SQLiteDatabase db = this.getWritableDatabase(); return db.query(TABLE_USER_BALANCE,columns,whereclause,whereargs,groupbyclause,null,null); } (显然以下会相应地改变)。

上述内容实际上并不返回特定用户/月的余额,但可能对其他方案有用。要返回余额(假设为双倍),则以下方法使用前一种方法在特定月份执行此操作: -

getUserBalanceAsCursor
  • 注意以上是原则上的代码,它尚未经过测试或运行,因此可能包含错误。
© www.soinside.com 2019 - 2024. All rights reserved.