Question

I have an URL pointing to content and I need to get highest value contained in one of the columns. Is there any aggregate function that will accomplish that or do I have to do this manually?

Was it helpful?

Solution

If you're querying an Android content provider, you should be able to achieve this by passing MAX(COLUMN_NAME) in to the selection parameter of ContentResolver.query:

getContentResolver().query(uri, projection, "MAX(COLUMN_NAME)", null, sortOrder);

Where Uri is the address of the content provider. This should return the single row with the highest value in COLUMN_NAME.

OTHER TIPS

Android's database uses SQLite, so SELECT MAX(thecolumn) FROM TheTable should work, just like in any other SQLite implementation (or for that matter any other SQL, "ite" or not;-). (If you're not using android.database you'd better specify what you're using instead;-).

The that worked for me.

Based on the responses of @Reto Meier and @Florian von Stosch.

public static long getMaxId(Context context) {

    long maxId = 0;

    Cursor maxCursor = context.getContentResolver().query(
            ProviderContentContract.CONTENT_URI,
            new String[]{"MAX(" + Table._ID + ")"},
            null,
            null,
            null);

    if (maxCursor != null && maxCursor.moveToFirst()) {
        maxId = maxCursor.getInt(0);
         maxCursor.close();
    }
    return maxId;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top