realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.46k stars 1.75k forks source link

GROUP BY #2309

Open cmelchior opened 8 years ago

cmelchior commented 8 years ago

We should add support for GROUP BY. Not entirely sure about the API as you usually want to supply a aggregate function at the same time. Also the return type will be a bit tricky, as the data is a lot more dynamic (and DynamicRealmObject doesn't fit). If we had a Sorted RealmMap it might make sense, although exposing as a List would probably be the more expected.

// Brainstorm ideas
RealmResults<Pair<String, Number>> results = query.groupBy("field", Function.AVERAGE);
RealmMap<String, Number> results = realmresult.groupBy("fieldName", Function.SUM);

http://stackoverflow.com/questions/35533550/is-it-possible-to-use-group-by-concept-in-realm-mobile-database

astigsen commented 8 years ago

I think there are tow very distinct use cases here. One is grouping because you need the objects within each group (like in segmented view), and the other one is the aggregates, which is more of a pivot table functionality.

The pivot table functionality is actually likely to be the easiest to implement, as there already are native support for that in core.

The grouping is a bit more difficult, mostly because of the question of what you should return. The most natural would be a RealmResults containing a new type, composed of the value being grouped on and a RealmList of the matching objects. This would either have to be accessed with the dynamic API, or the user should predeclare a matching type:

public class PersonGroupByAge extends RealmObject {
    private int age;
    private RealmList<Person> persons;
}

RealmResults< PersonGroupByAge > ages = realm.where(Person.class).groupBy("age").findAll();
sudhanshugaur4 commented 4 months ago

Is this issue Resolved now?