如何在android sqlite中使用日期和group bydate之间的两个不同表的字段求和与不同字段

时间:2019-11-04 10:12:12

标签: android sqlite android-sqlite

    public List<BillModel> Daily_Sales_Report(String from) {// passing the 

获取数据的日期,从两个表中按日期排列                     列表列表= new ArrayList();                     字符串query =“ select BILLING中的sum(Discount),sum(Del_ch),sum(Pkg_ch),substr(Created_Date,0,11),其中substr(Created_Date,0,11)就像'” + from +“'一样,由substr( Created_Date,0,11)“;                     字符串query1 =“ select count(*),sum(Amount),sum(gst_price),来自购物车的Created_Date,其中Created_Date类似于'” + from +“',按Created_Date”分组;

//here i used to cursor for getting the result from two different table and setting in the model class setter.
                Cursor cursor = this.getWritableDatabase().rawQuery(query, null);
                Cursor cursor1= this.getWritableDatabase().rawQuery(query1,null);
               if (cursor.moveToFirst()&& cursor1.moveToFirst()){// cursor and cursor1 will check the data from first positon 
                    String bill_date= cursor1.getString(3  ); //getting the date from table bill
                    String cart_date= cursor.getString(  3);//getting the cart_date from cart_table
                    try {
                        do {
                            if (bill_date==cart_date){ //if cart_date from cart_table ,and bill_date from bill_table will match then it will set in the setter other wise condition will check countiue in while loop                                   BillModel model = new BillModel(); //model class
                                 model.setbill_count( cursor1.getInt( 0 ) );   // here its for count total number of item
                                model.setC_Amount( cursor1.getFloat( 1 ) );
                                model.setB_total_gst( cursor1.getFloat( 2 ) );
                                model.setB_discount( cursor.getFloat( 0 ) );
                                model.setB_del_ch( cursor.getInt( 1 ) );
                                model.setB_pack_ch( cursor.getInt( 2 ) );
                                model.setB_create_date( cursor.getString( 3) );
                               list.add( model );
                            }
                       } while (cursor.moveToNext()&& cursor1.moveToNext());// using while loop for both the cursor
                    } catch (Exception e) {
                        Log.e( "Error", String.valueOf( e ) );
                    } finally {
                        cursor.close();
                        cursor1.close();
                    }
                }
                return list;
            }

i am trying to this,What's I am doing wrong here,not getting the result in model class variable. Is it write way or not?

2 个答案:

答案 0 :(得分:0)

保存日期的方式存在问题。根据SQLite,您的“ Created_Date”字段是一个字符串。

  

SQLite逐字母比较字符串

示例:

根据SQLite 01/11/2019少于21/10/2019,因为第一个日期以“ 0 ”开头,第二个日期以“ 2 ”开头'。


要解决此问题,您可以使用不同的日期格式,例如

  • yyyy / MM / dd :2019/11/01
  • yyyy / MM / dd HH:mm:ss :2019/11/01 14:21:43

答案 1 :(得分:0)

您得到的结果是对您编写的查询的正确响应。结果就是左联接的效果。

  

示例

没有“分组依据”的“左联接”的结果表将是

select * 
from CART left join BILLING on CART.index=BILLING.index 
where substr(BILLING.Created_Date,0,11) = '02/11/2019'

enter image description here

“ CART”中有两行的索引为1,而“ BILLING”中只有一行的索引为1。因此,当使用“左连接”时,帐单行将出现两次。 因此,当您使用“分组依据”时,这些值就会累加起来。