Querying records and making them groups from SQLite Table

94 Views Asked by At

I am not really understanding how can this be done in single query.

PROBLEM:

I have a table like this

id| phonenumber| message| group_name| datetime 

Example data

1 | 987654321 | "hi"    | G1 | xxxxxxx
2 | 987654321 | "hi"    | G1 | xxxxxxx
1 | 987145678 | "hello" | G2 | xxxxxxx

What I want to do is query the above SqlLite table in such a way that I need to grab all the rows of particular phonenumber in Descending order of datetime. So that I can put them in my HashMap with key as group_name and value as ArrayList of messages.

 HashMap<String, ArrayList<String>> mapper = new HashMap<>();

I am using GreenDao Library to fetch data from SqlLite, I tried like below

    List<activity> activities = activityDao.queryBuilder().where(new WhereCondition.StringCondition(com.ficean.android.ficean.db.activityDao.Properties.Contact_number.eq(phonenumber) + "GROUP BY group_name")).orderDesc(com.ficean.android.ficean.db.activityDao.Properties.Date_time).build().list();

I managed to do above query using GROUP BY but it is not listing all rows.Do I have to get All data of particular number and separate it looping through each row based on group_name ? or is there any better way to do?

1

There are 1 best solutions below

1
Gordon Linoff On

The SQL is quite simple, just a WHERE clause and an ORDER BY.

The SQL looks like this:

select t.*
from t
where t.phone = ?
order by t.datetime desc;

I am not sure how this translates into your SQL generator, but it does involve removing the GROUP BY.