acrolink

acrolink

How to do joins and grouping in Ecto

I know this can be achieved using raw SQL and Ecto.Adapters.SQL.query() but what is the Ecto way to it? Any help is highly appreciated, thank you.

SELECT COUNT(*) AS count, products.name AS product, categories.name AS category, stores.name AS store FROM "items"
  INNER JOIN "stores" ON "stores"."id" = "items"."store_id"
  INNER JOIN "products" ON "products"."id" = "items"."product_id"
  INNER JOIN "categories" ON "categories"."id" = "products"."category_id"
  WHERE "items"."store_id" IN (#{store_ids}) AND "items"."product_id"
		IN (#{product_ids}) AND (products.category_id IN (#{category_ids}))
  GROUP BY products.name, categories.name, stores.name
  ORDER BY products.name

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Let’s assume you’ve got ecto schemas with associations setup:

from i in Item,
  join: s in assoc(i, :store),
  join: p in assoc(, :product),
  join: c in assoc(p, :category),
  where: i.store_id in ^store_ids and i.product_id in ^product_ids,
  where: p.category_id in ^category_ids,
  group_by: {p.name, c.name, s.name},
  order_by: p.name,
  select: %{count: count(p.name), product: p.name, category: c.name, store: s.name}

It’s basically an exact translation. The only difference is that I think Ecto makes you more explicit about what you’re counting.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31194 112
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36432 110
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127536 1222
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

We're in Beta

About us Mission Statement