1

I created a table like this :

id   price   count    product_id

1    100      1           1
2    120      3           3
...

now I would like to get sum of all multiple count and price .

for this example :

(100 * 1) + (120 * 3) = 460

what I want is : 460

is it possible to use only the sql ?

S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254

2 Answers2

4

Actually I have same issue. you can try using this Query

Cursor cur = db.rawQuery("SELECT SUM(count * price) FROM myTable", null);
if(cur.moveToFirst())
{
    return cur.getInt(0);
}

This was work for me.

Rujul Gandhi
  • 1,700
  • 13
  • 28
-1

If you use the total often, you can store the result of the multiplication in a calculated column. Then you only need to sum that column.

Community
  • 1
  • 1
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268