5

I cant able to write a query to get distinct values in green dao's Objectbox database in android

Anil Kumar GN
  • 339
  • 1
  • 8
  • are you looking for distinct values for a (one) specific property? – Markus Junginger Oct 30 '17 at 12:55
  • yes , i need distinct years stored in database – Anil Kumar GN Oct 30 '17 at 13:15
  • For new researchers, this property is now added in ObjectBox: `String[] names = userBox.query().build().property(User_.firstName).distinct().findStrings();` from [here](https://objectbox.io/objectbox-1-4-property-queries-entity-inheritance/) – Ahmed Sep 16 '21 at 09:22

2 Answers2

1

Getting distinct values is tracked via this feature request: https://github.com/objectbox/objectbox-java/issues/198

We are currently collecting requirements, so do not hesitate to comment there.

Markus Junginger
  • 6,950
  • 31
  • 52
0

No idea if there are any BuildIn methods to do the same thing, but i was able to do count of distinct value using just SQL. may be it's help you.

String query = "SELECT COUNT (DISTINCT "
                                + HomeDao.Properties.VisiblePageId.columnName+
                                ") from "
                                + HomeDao.TABLENAME
                                + " where "
                                + HomeDao.Properties.IsVisible.columnName + " = 1 and "
                                + HomeDao.Properties.IsActive.columnName + " = 1";
                        Integer count = 0;

                        Cursor cursor =
                                MainApplication.getInstance().getDaoSession().getDatabase().rawQuery(
                                        query, null
                                );

                        if(cursor.moveToFirst()){
                            count = cursor.getInt(0);
                        }
                        cursor.close();
Munir
  • 2,548
  • 1
  • 11
  • 20
  • Hi, Distinct option is there in greendao but in BoxStore objectbox i dint find any method anyhow thanks for your response – Anil Kumar GN Oct 30 '17 at 12:28