I’ve tried “fragments” but I can’t get this work as “inserted_at” is a naive datetime, and my field consist only year and month so I can’t compare them in query.
What I can do is to select distinct year/month from query using fragments, but can’t get this work in “where” clause.
I want to get only entries from a given month, list all posts made in that month and sum comments for them.
No. You cannot use arbitrary elixir code in queries. Ecto queries are just a DSL to build up sql, so you can only do what your db supports using sql.
Something like
from p in Post,
where: fragment("date_part(?, ?)", "year", p.inserted_at) == ^year,
where: fragment("date_part(?, ?)", "month", p.inserted_at) == ^month