title: SQLite SUM query in Java on Android
body: <p>Android and Java beginners. I'm posting a question because there's something I want to implement but can't
  I received the price value by dividing the order into 1 and 2 and sub into 1 and 2 and 3</p>
  <p>I want to find two values using sum function in db.</p>
  <p>total1 = (total sum of price values with order 1 and sub = 1) - (total sum of price values with order 1 and sub = 2) - (total sum of price values with order 1 and sub = 3)
  Total2 = (total sum of price values with order 2 and sub = 1) - (total sum of price values with order 2 and sub = 2) - (total sum of price values with order 2 and sub = 3)</p>
  <p>I want to get two values and put total1 and total2 in the textview.</p>
  <p>First, I have to write a query in the database, but I don't know what to do. I wonder if it is right to use the cursor like this.</p>
  <p><a href="https://i.stack.imgur.com/Vh1pR.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p><a href="https://i.stack.imgur.com/yRxj8.png" rel="nofollow noreferrer">enter image description here</a></p>
  <pre><code> public int Calsum1() {
          SQLiteDatabase db = this.getWritableDatabase();
          int a=0, b=0, c=0, tot1;
  
          Cursor cursor1 = db.rawQuery(&quot;SELECT SUM(&quot; + (DBHelper.COLUMN_CH_Price) + &quot;) FROM &quot; + DBHelper.TABLE_CHOICE
                                  +&quot; WHERE &quot; +DBHelper.COLUMN_CH_SUB = 1 &amp;&amp; DBHelper.COLUMN_CH_SUB = 1, null);
          if(cursor1.moveToFirst()) {
             return a =cursor1.getInt(4);
          }
  
          Cursor cursor2 = db.rawQuery(&quot;SELECT SUM(&quot; + (DBHelper.COLUMN_CH_Price) + &quot;) FROM &quot; + DBHelper.TABLE_CHOICE
                  +&quot; WHERE &quot; +DBHelper.COLUMN_CH_SUB = 1 &amp;&amp; DBHelper.COLUMN_CH_SUB = 2, null);
          if(cursor2.moveToFirst()) {
              return b =cursor2.getInt(4);
          }
  
          Cursor cursor3 = db.rawQuery(&quot;SELECT SUM(&quot; + (DBHelper.COLUMN_CH_Price) + &quot;) FROM &quot; + DBHelper.TABLE_CHOICE
                  +&quot; WHERE &quot; +DBHelper.COLUMN_CH_SUB = 1 &amp;&amp; DBHelper.COLUMN_CH_SUB = 3, null);
          if(cursor3.moveToFirst()) {
              return c =cursor3.getInt(4);
          }
  
          tot1= a-(b+c);
          return tot1;
      }
  </code></pre>