There was recently a grouping question on the SQLite mailing list.
I have a complex query result set RESULT_TABLE that returns:
G V P ___________________ A X 1 A X 2 B Y 4 B Z 2 B X 8 C Y 6 C Z 8 C X 9 C Y 11G – defines groups
V – some value for that specific row
C – defined a display priority – i.e.I need to define a query that returns only the “first” row in each group – i.e. the row with the lowest display priority:
G V P ___________________ A X 1 B Z 2 C Y 6most preferably a query that doesn’t require selecting RESULT_TABLE more than once.
One response contained a familiar-looking pattern…
This solution may is tricky but has occasionally helped me. It is written here dedicated for the example data. For real data the leftpadding should likely be increased to the content of the sorting key. Also the result may need to be converted to the expected data type, it has now become text.
SELECT g, SUBSTR (MAX (SUBSTR (' ' || p, -2, 2) || v), 3, 1) v FROM t GROUP BY g;