I have this function that I pipe my query through:
def group_by_reference_id_and_date(query) do from(n in query, group_by: [ fragment("date_trunc(?, ?)", "day", n.inserted_at), n.reference_id, n.reference ], select: fragment("array_agg(DISTINCT ?)", n.id) ) end
but I am not able to order the groups in any way, is that possible to do?
I would want to order it by n.inserted_at, taking the latest inserted_at from each group, so the groups would be in a descending chronological order.