I understand why you wouldn’t want months to be ordered alphabetically, though I’m not sure what your desired ordering is…? February before January? November before September?
I’m going to answer as though you want things grouped by month (not year-month) and in month order. You can correct me from there.
If you look at the documentation for Postgres (which, as I cite it, you never mentioned explicitly Postgres, but… ), you see that there’s also an option to get “month number (01-12)” by using ‘MM’. If you want to order by month number, but still get month name, you could do…
from t in Table,
select: [fragment("to_char(?, 'Mon') as month", t.inserted_at), fragment("to_char(?, 'MM') as month_order", t.inserted_at), count(t.id)],
group_by: [fragment("month_order"), fragment("month")]
The data I get from this query (on my own dataset) looks like…
["Jan", "01", 92],
["Feb", "02", 68],
["Nov", "11", 14],
["Dec", "12", 11]
I realize you’re not particularly interested in month number, but I believe if you’re going to sort by it, you have to select it. If you really want to get fancy, you can use Enum functions to reformat those results in some way.