# Join with array - from sql to ecto?

**URL:** <https://forum.elixirforum.com/t/join-with-array-from-sql-to-ecto/23386>\
**Category:** Questions\
**Tags:** ecto, ecto-query\
**Created:** [June 25, 2019, 9:17am UTC](https://forum.elixirforum.com/t/join-with-array-from-sql-to-ecto/23386 "2019-06-25T09:17:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/max/32/10962_2.png) [@Max](https://forum.elixirforum.com/u/Max)\
**Post date:** [June 25, 2019, 9:17am UTC](https://forum.elixirforum.com/t/join-with-array-from-sql-to-ecto/23386/1 "2019-06-25T09:17:49Z")

</div>

Hi,

i´ve a working query with:

```sql
select u.id, u.lastname, array_agg(t.name)
from users as a
cross join unnest(tag_ids) as tag
join tags t on t.id = tag
group by u.id;

```

How would i put this with ecto?

So far i have found out i probably (not sure though) need something like:

```ex
fragment("unnest(?)", u.tag_ids) 
fragment("array_agg(DISTINCT ?)", t.name)

```

Problem: I don´t know how and where to put it.

Please help me.

---

<div class="post-metadata">

**Author:** ![wolfiton](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/wolfiton/32/15884_2.png) [@wolfiton](https://forum.elixirforum.com/u/wolfiton)\
**Post date:** [June 25, 2019, 9:34am UTC](https://forum.elixirforum.com/t/join-with-array-from-sql-to-ecto/23386/2 "2019-06-25T09:34:16Z")

</div>

I am new to this too but I found a blog post where the ecto joins are being explained. I am not sure if the way described in the article is the best way or a best practice.

Here is the link [SQL joins with Ecto](https://www.pompecki.com/post/sql-joins-with-ecto/)

Hope it helps you.

---

<div class="post-metadata">

**Author:** ![Max](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/max/32/10962_2.png) [@Max](https://forum.elixirforum.com/u/Max)\
**Post date:** [June 25, 2019, 9:35am UTC](https://forum.elixirforum.com/t/join-with-array-from-sql-to-ecto/23386/3 "2019-06-25T09:35:45Z")

</div>

Thanks, but this special case is not mentioned there.

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/32/18942_2.png) [@hauleth](https://forum.elixirforum.com/u/hauleth)\
**Post date:** [June 25, 2019, 9:37am UTC](https://forum.elixirforum.com/t/join-with-array-from-sql-to-ecto/23386/4 "2019-06-25T09:37:16Z")

</div>

Are you 100% sure you need `CROSS JOIN`? Because from what I see you need `INNER JOIN tags t ON t.id = ANY(tag_ids)` and this is supported by Ecto via `in/2`.

---

<div class="post-metadata">

**Author:** ![Max](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/max/32/10962_2.png) [@Max](https://forum.elixirforum.com/u/Max)\
**Post date:** [June 25, 2019, 10:10am UTC](https://forum.elixirforum.com/t/join-with-array-from-sql-to-ecto/23386/5 "2019-06-25T10:10:46Z")

</div>

@hauleth no 🙂

thank you
