Android Greendao - group and order by

329 Views Asked by At

I'm using GreenDAO in my project.

I have a chats table which contains all the messages. I want to group by the chat_id and order the messages in descending order by the timestamp of the message.

so what I'm trying to get is a list that contains the most recent message of each chat the user has.

this is the code I'm using:

Chats.queryBuilder()
     .orderDesc(ChatsDAO.Properties.TimeStamp)
     .where(ChatsDAO.Properties.UserId.eq(user_id))
     .where(new WhereCondition.StringCondition("1 GROUP BY chat_id"))
     .list();

but still, the code above gives me a list which contains the first message of each chat, instead of the recent message.

is there any way to make a raw query using greenDAO and select what I want?

1

There are 1 best solutions below

4
Muhammad Awais On

You can make a raw query using GreenDAO as follow. Also, check the link

 Query<User> query = userDao.queryBuilder().where(
 new StringCondition("_ID IN " +
 "(SELECT USER_ID FROM USER_MESSAGE WHERE READ_FLAG = 0)")
 ).build();