ObjectBox, sum a property by grouping another property

372 views Asked by At

Imagine I have this table:

tagId amount
1 100
1 150
2 200
2 250

And i need to sum the amount by grouping the tagId.

In SQL we can do this by: SELECT tagId, SUM(amount) FROM orders GROUP BY tagId;

Query result:

tagId SUM(amount)
1 250
2 450

How can I get such results in ObjectBox ?

1

There are 1 answers

2
Uwe - ObjectBox On

There is no GROUP function in ObjectBox. Instead, write code to process query results in which ever way you desire. There are built-in property queries that can return a sum that may be helpful:

val sum = tagBox.query(Tag_.id.equals(id)).build()
    .property(Tag_.amount)
    .sumDouble()

Source: https://docs.objectbox.io/queries#propertyquery