在sqlite数据库查询中重复相同的值

时间:2016-10-19 08:34:17

标签: android sqlite

我有三个表格,如交易,预付款和贷款我必须创建一个列表,显示每个月的交易,贷款和预付款的总和,假设一个月没有交易意味着它应该显示为零,但不显示值为零,它显示下个月前几个月的总和值也可以告诉我在这个查询中我错在哪里,因为我对sqlite知之甚少。

SELECT strftime('%m-%Y', tradedate) as 'month', 
   (SELECT IFNULL (SUM(tradebalanceamount),0 ) 
       FROM farmertradelabel 
       WHERE farmertradelabel.mobileno = ' 9486032141 '  
       ORDER BY strftime('%m-%Y', farmertradelabel.tradedate) DESC) AS tradeamount,
  (SELECT IFNULL (SUM(advanceamount),0)  
       FROM advancelabel 
       WHERE advancelabel.mobileno = '9486032141'  
       ORDER BY strftime('%m-%Y', advancelabel.advancedate) DESC) AS advanceamount,
  (SELECT IFNULL (SUM(loantotalamount),0) 
      FROM loanlabel 
      WHERE loanlabel.mobileno = '9486032141' 
      ORDER BY strftime('%m-%Y', loanlabel.loandate) DESC) AS loanamount   
FROM farmertradelabel

表格结构:

交易表:

 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);

输出:

output

1 个答案:

答案 0 :(得分:0)

查看以下内容是否适用于systemadvancedamount字段。 基本上,您需要检查月份是当前行的月份,并将其SUM,否则将其排除。使用where子句。

loanamount

SELECT strftime('%m-%Y', tradedate) as 'month', (SELECT IFNULL (SUM(FTL.tradebalanceamount),0 ) FROM farmertradelabel as FTL WHERE FTL.mobileno = '9486032141' AND strftime('%m-%Y', FTL.tradedate) = strftime('%m-%Y', farmertradelabel.tradedate) ORDER BY strftime('%m-%Y', FTL.tradedate) DESC) AS tradeamount, (SELECT IFNULL (SUM(advanceamount),0) FROM advancelabel WHERE advancelabel.mobileno = '9486032141' AND strftime('%m-%Y', advancelabel.advancedate) = strftime('%m-%Y', farmertradelabel.tradedate) ORDER BY strftime('%m-%Y', advancelabel.advancedate) DESC) AS advanceamount, (SELECT IFNULL (SUM(loantotalamount),0) FROM loanlabel WHERE loanlabel.mobileno = '9486032141' AND strftime('%m-%Y', loanlabel.loandate) = strftime('%m-%Y', farmertradelabel.tradedate) ORDER BY strftime('%m-%Y', loanlabel.loandate) DESC) AS loanamount FROM farmertradelabel 要求您更进一步,添加Alias'到表中,因为您使用相同的表名两次。

上面的修改示例包含Alias'