Android and Java beginners. I'm posting a question because there's something I want to implement but can't I received the price value by dividing the order into 1 and 2 and sub into 1 and 2 and 3
I want to find two values using sum function in db.
total1 = (total sum of price values with order 1 and sub = 1) - (total sum of price values with order 1 and sub = 2) - (total sum of price values with order 1 and sub = 3) Total2 = (total sum of price values with order 2 and sub = 1) - (total sum of price values with order 2 and sub = 2) - (total sum of price values with order 2 and sub = 3)
I want to get two values and put total1 and total2 in the textview.
First, I have to write a query in the database, but I don't know what to do. I wonder if it is right to use the cursor like this.
public int Calsum1() {
SQLiteDatabase db = this.getWritableDatabase();
int a=0, b=0, c=0, tot1;
Cursor cursor1 = db.rawQuery("SELECT SUM(" + (DBHelper.COLUMN_CH_Price) + ") FROM " + DBHelper.TABLE_CHOICE
+" WHERE " +DBHelper.COLUMN_CH_SUB = 1 && DBHelper.COLUMN_CH_SUB = 1, null);
if(cursor1.moveToFirst()) {
return a =cursor1.getInt(4);
}
Cursor cursor2 = db.rawQuery("SELECT SUM(" + (DBHelper.COLUMN_CH_Price) + ") FROM " + DBHelper.TABLE_CHOICE
+" WHERE " +DBHelper.COLUMN_CH_SUB = 1 && DBHelper.COLUMN_CH_SUB = 2, null);
if(cursor2.moveToFirst()) {
return b =cursor2.getInt(4);
}
Cursor cursor3 = db.rawQuery("SELECT SUM(" + (DBHelper.COLUMN_CH_Price) + ") FROM " + DBHelper.TABLE_CHOICE
+" WHERE " +DBHelper.COLUMN_CH_SUB = 1 && DBHelper.COLUMN_CH_SUB = 3, null);
if(cursor3.moveToFirst()) {
return c =cursor3.getInt(4);
}
tot1= a-(b+c);
return tot1;
}
