I have made a content provider for my application. For the sake of simplicity, I shall use an example. I have a table 'OrderDetails'.
- I have a column for the OrderId.
- I have a column for the quantity of the different product types that was purchased in that OrderId.
- I have an OrderDetailId that is the primary key for the table.
I want to write the query: SELECT OrderID, sum(quantity) FROM OrderDetails GROUP BY OrderID
Which would return this:
However, I have tried to insert my GROUP BY clause in my URI for my content Provider but It will not work, so the resulting query becomes: SELECT OrderID, sum(quantity) FROM OrderDetails
that returns this (the entire quanity of everything and the last OrderId):
Here is the method to get the cursor and simply print out the result that I have just made:
private void findQuantityByOrder(){
Uri uri = DatabaseContract.CONTENT_URI_ORDER_DETAILS;
String[] selection = new String[] {DatabaseContract.DatabaseOrderDetails.ORDER_ID,
"sum("+ DatabaseContract.DatabaseOrderDetails.QUANTITY + ")"
+"GROUP BY(" + DatabaseContract.DatabaseOrderDetails.ORDER_ID + ")"};
String projection = null;
String sortBy = null;
String[] args = null;
Cursor cursor = getContentResolver().query(
uri,
selection,
projection,
args,
sortBy);
for (int i = 0; i < cursor.getCount(); i ++) {
cursor.moveToPosition(i);
int orderID = cursor.getInt(cursor.getColumnIndex("orderID"));
int quantity = cursor.getInt(cursor.getColumnIndex("sum(quantity)"));
System.out.println("orderId: " + orderID + ". Quanitity: " + quantity);
}
}
It only prints out the entire sum of all orders, with the last ID that was on the table.
I believe that GROUP BY was removed and no longer supported. Is there any other way that will provide the same result?
Thank you