简体   繁体   中英

How to use SUM and DINSTINCT with GreenDao querybuilder?

I want to get the sum of total rows in a column of a table in SqlLite Database. I am using GreenDao Library. I know I Have to use SUM . But I don't know how to use it with GreenDao queryBuilder . Can anybody help me? and also DISTINCT.

This is how I did it. Assume there is the Company entity and it has the numOfEmployees field:

Cursor cursor = daoSession.getDatabase().rawQuery("SELECT SUM(" + Company Dao.Properties.NumOfEmployees.columnName + ") FROM " + Company Dao.TABLENAME, new String []{});
cursor.moveToFirst();
long result = cursor.getLong(0);

Note that you could hardcode the column and table names above, but this is way cleaner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM