SQL Query Select Count(*) AS grouped

63 views Asked by At

can someone help me to take the number of grouped products from a table to pass them to a recyclerview? My code works fine with a simple query, but when I want to count grouped data it doesn't work I hope you can help me. thank you so much!!

I am working with Android Studio Giraffe | 2022.3.1 Patch 2

This is the part of the code that I can't solve

public class DbDBMaster extends DbHelper {

    Context context;

    
   public ArrayList<Products> takeAmount() {

        DbHelper dbHelper = new DbHelper(context);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ArrayList<Products> listProducts= new ArrayList<>();
        Products products;
        Cursor cursorProducts;
    // This works fine: cursorProducts = db.rawQuery("SELECT * FROM " + TABLE_MYDBProducts+ " GROUP BY numberProd ", null);
        cursorProducts = db.rawQuery("SELECT COUNT(*) AS amount,numberProd, nameProd FROM " + TABLE_MYDBProducts+ " GROUP BY numberProd ", null);

        if (cursorProducts.moveToFirst()) {
            do {
                products = new Products();
                products.setAmount(cursorProducts.getInt(0));
                products.setNumberProd(cursorProducts.getString(1));
                products.setNameProd(cursorProducts.getString(2));
                listProducts.add(products);
            } while (cursorProducts.moveToNext());
        }
        cursorProducts.close();
        return listProducts;
    }
}

when I want to count grouped data it doesn't work

1

There are 1 answers

1
Sam On BEST ANSWER

Try the following:

GROUP BY numberProd, nameProd

And let us know if it works