Suppose I have a SQL table "songs" with three columns: "title", "artist" and "ranking":
track | artist | ranking
---------+--------+---------
A | A1 | 3
B | A1 | 2
C | A1 | 1
D | A2 | 4
E | A3 | 7
F | A3 | 6
G | A3 | 5
I want to make a SELECT query that will return just the first N (e.g. two) row for each artist while ordering by the ranking. I'm aware that using GroupBy on the artist column will return one row of each artist, however I want two in this scenario like so:
track | artist | ranking
---------+--------+---------
A | A1 | 1
B | A1 | 2
D | A2 | 4
E | A3 | 5
F | A3 | 6
If it is possible, I would preferably want to construct this within android using a queryBuilder, this is as close as I managed to get:
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
String[] sqlSelect = {"title", "artist", "ranking"};
String sqlTable = "songs";
String orderBy = "ranking DESC";
qBuilder.setTables(sqlTable);
cur = qBuilder.query(db, sqlSelect, null, null, null, null, orderBy);
I have seen related questions, however I haven't been able to find a way to apply it to this scenario - any direction on how to accomplish this would be appreciated.