Handling List-types with Esqueleto
Esqueleto is NOT meant to handle list of sublists (multidimensional list) out of the box yet! Data.List.groupBy
that 'cdk' advised to you can group only list itself, but not what you was asking for.
For your case I would insistently advise you to use a classic SQL queries. You can run n+1 queries, but do that only if it is rare and not often usable function, that for example prepares cached data (based on your variables names I suppose that it may not be heavy used and it worth a try). For heavy usage you should consider using classic SQL without any doubt.
If you will go to https://github.com/prowdsponsor/esqueleto you will find that:
Not all SQL features are available, but most of them can be easily added (especially functions).
so you can try to ask for a new feature. Good luck!