Skip to content
Advertisement

Sqlite SUM Entity value depending on LEFT JOIN value

I’m using Room on Android to query a POJO.

My POJO (Transaction) contains a value and an account_id. It further contains an account (instance of Account), which is fetched by a @Relation.

The transaction class:

The accounts properties:

The accounts.balance is not a value in the database, but the sum of all transactions.value belonging to that account.

So I need to sum every transaction.value where transaction.account_id = account.id.

It should work similar to this way, but I’m not getting it right:

Advertisement

Answer

I’ve made it by creating a new POJO wrapping my original account object, but adding a field for the sum which is now queried.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement