i am using cursor for retrieving data from database but problem is that i have no. of records for same date and i want only one record date wise and related Tamt of date will be added to amount field:
I have database records like :
Sr ExpName Date Camt Tamt
--- --------- ----------- ----- -----
1 Pen 10-10-2012 10 10
2 abc 10-10-2012 30 45
3 xyz 11-10-2012 15 55
4 ggg 11-10-2012 20 75
5 aaa 11-10-2012 10 85
6 nnn 11-10-2012 10 95
dbhelper = new DbHelper(this);
db = dbhelper.getReadableDatabase();
Cursor c = db.query(DbHelper.TABLE_NAME, new String[]{"CAST(Date AS DATE)","SUM(Tamt) AS AMT"}, null, null,"CAST(Date AS DATE)", null, null);
adapter = new SimpleCursorAdapter(this, R.layout.myrow1, c, new String[]{DbHelper.C_Date,DbHelper.C_Tamt},new int[]{R.id.textView3,R.id.textView5} );
startManagingCursor(c);
lstExpense.setAdapter(adapter);
where myrow1 is a layout file in and TextView3 and TextVew5 field where i want to display output DATE and AMOUNT.
And i want ouput like this:
DATE AMOUNT
------------ ------
10-10-2012 40
11-10-2012 35
12-10-2012 10
13-10-2012 10
This sounds like a problem for GROUP BY and the SUM aggregation function.
Working with SQLite directly your query would need to be along the lines of
(I’ve assumed that your database table is called
Expenses.)Converting this into an Android cursor query is then pretty simple
That should give you the result that you’re looking for.