1

I have a table called transactions with the following schema:

ID  |   P_Name   |  O_ID |   P_ID  |   P_Qty   |
===============================================
1   | PRODUCT A  |   1   |   140   |   100     |
2   | PRODUCT A  |   1   |   140   |   15      |
3   | PRODUCT B  |   1   |   130   |   10      |
4   | PRODUCT C  |   1   |   120   |   2       |

I want to sum all P_Qty that share a P_Name so I can eventually end up with:

P_Name    | P_ID  |  P_Qty    |
===============================
PRODUCT A | 140   |   115     |
PRODUCT B | 130   |   10      |
PRODUCT C | 120   |   2       |

As you can see the P_Qty is summed where the P_ID is same, I am using a ContentResolver as shown below its however failing as shown:

    String selection = null;

    String[] selectionArgs = null;

    String orderBy = TRANSACTION_PRODUCT_NAME  + " ASC";

    Cursor cursor = getActivity().getContentResolver().query(
            SalesManContract.TRANSACTION_CONTENT_URI,
            new String[] {TRANSACTION_PRODUCT_NAME," SUM(P_Qty) AS 
            P_Qty"},
            selection,
            selectionArgs,
            orderBy);

The result is:

 P_Name    | P_ID  |  P_Qty    |
===============================
PRODUCT A | 140   |   127     |

How can I achieve my desired result using ContentResolver

James Wahome
  • 588
  • 11
  • 31

0 Answers0