I have a database created and filed with data. There is a column where i want to find the biggest value.
This is the method used in my database adapter :
public Cursor getBiggestInTheColumn() { return db.query(DATABASE_TABLE, null, "MAX(price)", null, null, null, null); }
It should work but when i call the method :
cursor = dbadp.getBiggestInTheColumn();
I Get a Runtime Error Like This(LogCat) :
07-14 12:38:51.852: ERROR/AndroidRuntime(276): Caused by: android.database.sqlite.SQLiteException: misuse of aggregate function MAX(): , while compiling: SELECT * FROM spendings WHERE MAX(price)
Any ideas? I suspect this is due to the bad query, but this is the best that i can come up with. Other queries are working well.
To find the max value of a column, use the MAX() aggregate function; it takes as its argument the name of the column for which you want to find the maximum value. If you have not specified any other columns in the SELECT clause, the maximum will be calculated for all records in the table.
The SQLite MAX function is an aggregate function that returns the maximum value of all values in a group. You can use the MAX function to accomplish a lot of things. For example, you can use the MAX function to find the most expensive products, find the biggest item in its group, etc.
MAX can be used with numeric, character, and datetime columns, but not with bit columns.
Maximum Database Size 140 tb but it will depends on your device disk size.
If you just want the biggest value then you want the SQL equivalent of: SELECT MAX(price) FROM spendings
.
I tend to use db.rawQuery(String sql, String[] selectionArgs)
, so it would be: db.rawQuery("SELECT MAX(price) FROM spendings", null)
.
But I think you could do this with db.query(DATABASE_TABLE, new String [] {"MAX(price)"}, null, null, null, null, null);
. Though I haven't tested that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With