# How to get the top X results of a given category using Ecto?

**URL:** <https://forum.devtalk.com/t/how-to-get-the-top-x-results-of-a-given-category-using-ecto/95919>\
**Category:** Backend Questions/Help\
**Tags:** ecto, elixir\
**Created:** [10 February 2023 09:22 UTC](https://forum.devtalk.com/t/how-to-get-the-top-x-results-of-a-given-category-using-ecto/95919 "2023-02-10T09:22:48Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Fl4m3Ph03n1x](https://forum.devtalk.com/user_avatar/forum.devtalk.com/fl4m3ph03n1x/32/3808_2.png) [@Fl4m3Ph03n1x](https://forum.devtalk.com/u/Fl4m3Ph03n1x)\
**Post date:** [10 February 2023 13:11 UTC](https://forum.devtalk.com/t/how-to-get-the-top-x-results-of-a-given-category-using-ecto/95919/3 "2023-02-10T13:11:21Z")

</div>

# Solution(s)

From what I gathered, there are two decent possible solutions to this conundrum.

### `row_number` + `over`

One of them is using `row_number()` with `over()` from Ecto:

> **[from p in Post, select: row\_number() |\> over(partition\_by: p.category\_id,... -...](https://hexdocs.pm/ecto/Ecto.Query.WindowAPI.html#row_number/0)**
>
> Calculates the average for the given entry.

Assuming I join both `file_info` with `all_counts` in a single table, I can then perform the query as mentioned in the previous SO post I mentioned in my question:

```auto
file_info_with_counts
|> select([fi], %{
        rn: over(row_number(), partition_by: [fi.home_id, fi.path], order_by: [asc: fi.item_id]),
        item_id: fi.item_id,
       # you get the idea ...
      })
|> subquery()

IO.inspect(file_info_with_counts |> where([c], c.rn <= 3) |> Repo.all()

```

Which prints what I wanted.

Source:

> **[How to get the top X results of a given category using Ecto?](https://elixirforum.com/t/how-to-get-the-top-x-results-of-a-given-category-using-ecto/53805/9?u=fl4m3ph03n1x)**
>
> Repo.insert\_all(ExampleRecord, \[\[name: "John", value: 20\], \[name: "John", value: 7\], \[name: "John", value: 7\], \[name: "John", value: 7\], \[name: "John", value: 4\] \]) table = Ecto.Query.from(t in...

### Lateral inner joins

However, as mentioned by some people in the community, this solution is rather old, and these days lateral joins seem to also cover this use case.

```auto
tops = 
  from top in "file_info_with_counts", 
    where: top.home_id == parent_as(:parent).home_id,
    where: top.path == parent_as(:parent).path,
    order_by: [asc: top.item_id],
    limit: 3,
    select: %{id: top.id}

from parent in "file_info_with_counts",
  as: :parent,
  group_by: [parent.home_id, parent.path],
  lateral_join: top in subquery(tops), 
  on: true,
  select: %{home_id: parent.home_id, path: parent.path, item_id: top.id}

```

Source:

> **[How to get the top X results of a given category using Ecto?](https://elixirforum.com/t/how-to-get-the-top-x-results-of-a-given-category-using-ecto/53805/11?u=fl4m3ph03n1x)**
>
> I don’t think you need the first subquery here: tops = from top in "file\_info\_with\_counts", where: top.home\_id == parent\_as(:parent).home\_id, where: top.path == parent\_as(:parent).path, order\_by: \[asc: top.item\_id\], limit: 3, ...

This solution is not without merit, however, given my familiarity with `row_number` I opted for that solution instead.

Unless there is a considerable performance difference between the two in favor of lateral joins, I will keep the previous solution.

For more info, here is the original source where I got these answers:

> **[How to get the top X results of a given category using Ecto?](https://elixirforum.com/t/how-to-get-the-top-x-results-of-a-given-category-using-ecto/53805/8)**
>
> Reading from this post about lateral joins: I believe what I want in SQL could be something like this: (assuming file\_info\_with\_counts is the inner join of all\_counts with file\_info): SELECT home\_id, path, first\_item, other\_items FROM ...

---

_[View the full topic](https://forum.devtalk.com/t/how-to-get-the-top-x-results-of-a-given-category-using-ecto/95919)._
