Yes, this works.
From the top of my head, it is even cleaner is using a plain count(), which will sum up all entries (in the particular group).
Theoretically the count(l.inserted_at) might skip records where inserted_at is nil, for instance. (As inserted_at is automatically managed by most Ecto schema’s, it’s unlikely that this is ever the case, but it is possible.)
hi. I have a question in this case that if inserted_at can be nil sometimes. how can we handle both cases? Can we use something like a guard function when inserted_at is nil then count all the queries where it is nil and similarly if it is not nil then count all the records for that?
Depends if you’re okay with 2 separate DB queries. If that’s fine then just make where clause’s for both cases. If you want only 1 query then use the above approach and count then programmatically (in the Elixir code).