数据从特定表的日期而不是sqlite中的所有表填充

时间:2016-10-19 12:18:54

标签: android sqlite android-sqlite

我有三张表,即交易,预付和贷款,当我在贷款中输入一些价值时,直到我在特定日期/月的交易中输入一些值时才会显示。

所以有人能告诉我如何克服这个问题吗?

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

1 个答案:

答案 0 :(得分:3)

尝试以下内容。

我所做的是创建一个包含唯一月份的虚拟表。该月的3个表中的一个表中有信息。然后总和就到了那个月。

我还删除了ORDER BY条款中的SUM,因为它们不会更改结果。您的IFNULL也可以更改,具体取决于您的预期效果。

此方法可以节省您为每3个字段(9个SUM)写出3批SUM。如果有任何拼写错误,请相应修改。

注意month是某个SQL中的关键字。理想情况下,应该使用更好的列名。

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

FROM
 (SELECT strftime('%m-%Y', tradedate) as 'month' FROM farmertradelabel
 UNION 
   SELECT  strftime('%m-%Y', advancedate) as 'month' FROM advancelabel
 UNION 
   SELECT strftime('%m-%Y', loandate) as 'month' FROM loanlabel
 ) as dateTable