# Ecto query call when using aggregate function

**URL:** <https://forum.elixirforum.com/t/ecto-query-call-when-using-aggregate-function/24900>\
**Category:** Questions\
**Tags:** ecto, phoenix, ecto-query\
**Created:** [August 24, 2019, 10:06pm UTC](https://forum.elixirforum.com/t/ecto-query-call-when-using-aggregate-function/24900 "2019-08-24T22:06:23Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Yama](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/y/c89c15/32.png) [@Yama](https://forum.elixirforum.com/u/Yama)\
**Post date:** [August 25, 2019, 1:39am UTC](https://forum.elixirforum.com/t/ecto-query-call-when-using-aggregate-function/24900/2 "2019-08-25T01:39:28Z")

</div>

I believe I may have found a solution. Would love some input if there is a better approach or maybe some flaws so what seems to be working. Thanks again

```elixir
  def get_tags(search) do
    subquery =
      from(pt in PostTag,
        select: %{tag_id: pt.tag_id, postCount: count(pt.tag_id)},
        group_by: pt.tag_id
      )

    query =
      from(t in Tag,
        join: pt in subquery(subquery),
        on: t.id == pt.tag_id,
        where: ilike(t.name, ^search),
        select: %{id: t.id, name: t.name, count: pt.postCount}
      )

    # from(t in Tag, join: p in assoc(:posts), select: t.name)
    Repo.all(query)
  end

```

---

_[View the full topic](https://forum.elixirforum.com/t/ecto-query-call-when-using-aggregate-function/24900)._
