androidgreendao

Greendao Distinct count query


So i have been strugglin to get this query to work with GreenDao, and my problem is the start of the query, which (using rawquery) starts after the where clause.

It it even possible to do this query with GreenDao or do i have to use standard SQL Queries accessing the database without using GreenDao?

select count (distinct VISIBLE_PAGE_ID) from HOME_ITEM2 where IS_VISIBLE=1 and IS_ACTIVE = 1

Solution

  • So i was able to do it using just SQL, no idea if there are any BuildIn methods to do the same thing:

      String query = "SELECT COUNT (DISTINCT "
                                        + HomeItem2Dao.Properties.VisiblePageId.columnName+
                                        ") from "
                                        + HomeItem2Dao.TABLENAME
                                        + " where "
                                        + HomeItem2Dao.Properties.IsVisible.columnName + " = 1 and "
                                        + HomeItem2Dao.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();