from post in Post,
group_by: post.status,
select: {post.status, count(post.id)}
result = query |> Repo.all()
# I didn't think too hard about this, so there is probably a better way to write the code that does this,
# but the point is that the next example is abundantly clear in comparison
total =
result
|> Enum.map(&elem(1))
|> Enum.sum()
result
|> Enum.into(%{})
|> Map.take(["active", "inactive", "deleted", "pending"])
|> Map.put("total", total)
query =
from post in Post,
select:%{
active: count(:id, filter: post.status == "active"),
inactive: count(:id, filter: post.status == "inactive"),
deleted: count(:id, filter: post.status == "deleted"),
pending: count(:id, filter: post.status == "pending"),
total: count(:id)
}
Repo.all(query)SELECT
COUNT(p.id) FILTER (WHERE p.status = "active") active,
COUNT(p.id) FILTER (WHERE p.status = "inactive") inactive,
COUNT(p.id) FILTER (WHERE p.status = "deleted") deleted,
COUNT(p.id) FILTER (WHERE p.status = "pending") pending,
COUNT(p.id) total
FROM posts--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/b04293a3-ac01-4ea1-91f8-761507136440%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/34d252a7-81ec-441f-8ccf-17dba93cf63a%40googlegroups.com.
from post in Post,
where: post.deleted == false,
select_merge: %{
active: count(post.id, where: post.status == "active")
},
where: post.special == true,
select_merge:%{
inactive: count(post.id, where: post.status == "inactive")
}--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/686b0d4d-d856-432e-a8c1-2352fc9b0dda%40googlegroups.com.
We could solve for all variations really.
`count(p.foo, :distinct)` `count(p.foo, distinct: true)` `count(p.foo, where: p.thing == true)` and `count(p.foo, :distinct, where: p.thing == true)` are all distinguishable. I think leaving it as a keyword last argument there leaves us open to supporting window functions and things like that some day (not that I necessarily think that is time well spent), but aggregates do support more options *aside* from FILTER in postgres, so keywords leave us open for that.
--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/781ead5a-8d8d-4b86-83b4-894be6ac47ee%40googlegroups.com.
The other idea here is to introduce filter as a separate function:filter(count(p.id), p.thing == true)This is probably a better choice because it works with all aggregators, including user ones, and it doesn't need to deprecate count with distinct. Reference: https://www.postgresql.org/docs/9.4/static/sql-expressions.html
Yes, I like that :)
Especially since the syntax there *is* very separate. like `agg()` and `agg() FILTER ()`, which means there are a lot more expressions than `count` that this plays nice with, including something like `filter(fragment("array_agg(?)", p.foo), p.thing == true)`
--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/c91494dc-b626-453d-ad5d-3a774615dba4%40googlegroups.com.
select: %{
count_of_active: count(p.foo) |> filter(p.active == true),
count_of_inactive: count(p.foo) |> filter(p.active == false)
}--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/e7677006-0107-4521-be7e-d862e83d6d0c%40googlegroups.com.