如何在sqlite中对数据进行分组

时间:2016-10-18 04:29:07

标签: android sqlite

我在sqlite交易,贷款和预付款中有三个表格,其中我想总结每个月每个表格的所有总价值,假设当前月份的贷款记录为零,那么它应该显示为0但是当我使用内部联接概念时,价值变得加倍,贷款金额显示总贷款

交易表:

String CREATE_FARMERTRADE_TABLE = "CREATE TABLE " + FARMERTRADE_LABELS + "("
            + TRADE_ID + " INTEGER PRIMARY KEY,"
            + TRADE_DATE + " TEXT,"
            + TRADE_MOBNO + " TEXT,"
            + TRADE_COCONUTTYPE + " TEXT,"
            + TRADE_TOTALCOCONUT + " TEXT,"
            + TRADE_COCONUTCOST + " TEXT,"
            + TRADE_TOTALAMOUNT + " TEXT,"
            + TRADE_AMOUNTPAID + " TEXT,"
            + TRADE_BALANCEAMT + " TEXT" + ");";
    db.execSQL(CREATE_FARMERTRADE_TABLE);

高级表:

String CREATE_ADVANCE_TABLE = "CREATE TABLE " + ADVANCE_LABELS + "("
            + ADVANCE_ID + " INTEGER,"
            + ADVANCE_DATE + " TEXT,"
            + ADVANCE_USERTYPE + " TEXT,"
            + ADVANCE_MOBNO + " TEXT PRIMARY KEY,"
            + ADVANCE_AMOUNT + " TEXT,"
            + ADVANCE_REASON + " TEXT" + ");";
    db.execSQL(CREATE_ADVANCE_TABLE);

贷款表:

String CREATE_LOAN_TABLE = "CREATE TABLE " + LOAN_LABELS + "("
            + LOAN_ID + " INTEGER,"
            + LOAN_DATE + " TEXT,"
            + LOAN_USERTYPE + " TEXT,"
            + LOAN_MOBNO + " TEXT PRIMARY KEY,"
            + LOAN_AMOUNT + " TEXT,"
            + LOAN_INTEREST + " TEXT,"
            + LOAN_PERIOD + " TEXT,"
            + LOAN_DUEDATE + " TEXT,"
            + LOAN_REASON + " TEXT,"
            + LOAN_TOTALAMOUNT + " TEXT" + ");";
    db.execSQL(CREATE_LOAN_TABLE);

查询:

 String selectQuery = "SELECT SUM(tradetotalamount) as totalamount, SUM(tradebalanceamount) as balanceamt, strftime('%m-%Y', tradedate) as 'month',SUM(advanceamount) as advanceamt, SUM(loanamout) as loanamt "+
            " FROM " + FARMERTRADE_LABELS + " INNER JOIN advancelabel ON farmertradelabel.mobileno = advancelabel.mobileno INNER JOIN loanlabel ON loanlabel.mobileno = farmertradelabel.mobileno  WHERE " + FARMERTRADE_LABELS + "." + TRADE_MOBNO + " = " + mobno + " " +
            "AND tradedate BETWEEN (select min(tradedate) from farmertradelabel) AND (select max(tradedate) from farmertradelabel)" +
            " GROUP BY strftime('%m-%Y', farmertradelabel.tradedate) ORDER BY strftime('%m-%Y', farmertradelabel.tradedate) DESC ";

如果某个特定的表没有该特定月份的任何值,那么我如何在单个表中选择所有三个表值?然后我如何将该值显示为零?。谢谢

0 个答案:

没有答案