tonarie

tonarie

Grouping and sorting - column must appear in the GROUP BY issues

Hello, first post here. Sorry I’m not good at explaining things.

I have a table of Buses, each of which is associated with one or more Stops and Routes.
I’m trying to make a searchable list of all stops, but I don’t want to re-write my searching and filtering functions see (list_filtered_buses).

So, in sort_bus_list, I’m trying to return a sorted list of all stops.
My issue with the current implementation is that I get back duplicates when I render the list in my template since each bus is being returned with all its stops.

Here is relevant code in sort_bus_list in buses.ex
#V1 - doesn’t return duplicates, but isn’t returns error:
# ERROR 42803 (grouping_error): column “b1.stop_id” must appear in the GROUP BY clause or be used in an aggregate function
list
|> order_by([b, s, r], {^attrs.sort_dir, field(s, ^attrs.sort_attr)})
|> group_by([b, s, r], b.id)

  #V2 - sortable, but returns duplcate

list
|> order_by([b, s, r], {^attrs.sort_dir, field(s, ^attrs.sort_attr)})
|> group_by([b, s, r], [s.id, b.id])

Here’s sudo code of my template:
for bus in Buses
for stop in bus
display stop
end
end

More Buses.ex code for context:
def list_filtered_buses(params) do
search_term = params[“filter”][“query”]

    from(t in bus, join: a in assoc(t, :stops), join: c in assoc(t, :routes))
    |> search(search_term)
    |> list_buses_by_active(params)
    |> list_buses_by_type(params)
    |> sort_bus_list(params)
    |> preload([:stops, :routes])
  end

  def sort_bus_list(list, params) do
    attrs = process_sorting_metrics(params)

    case params["filter"]["sort_attr"] do
      a when a == "stop_name" or a == "stop_id" ->

      #V2 - sortable, but returns duplicate
      list
        |> order_by([b, s, r], {^attrs.sort_dir, field(s, ^attrs.sort_attr)})
        |> group_by([b, s, r], [s.id, b.id])
 
      "route_name" ->
        list
        |> order_by([b, s, r], {^attrs.sort_dir, field(r, ^attrs.sort_attr)})
        |> group_by([b, s, r], [b.id, r.id])

      _ ->
        list
        |> order_by([b, s, r], {^attrs.sort_dir, field(b, ^attrs.sort_attr)})
        |> group_by([b, s, r], b.id)
    end
  end

Most Liked

peerreynders

peerreynders

Ulitimately that error is neither an Ecto nor an Elixir error but a SQL error.

When learning SQL you don’t start with aggregate queries, it’s considered a more advanced skill and before using them in Ecto it does help considerably to have a good handle on matters in the SQL space.

http://www.postgresqltutorial.com/postgresql-group-by/

SELECT
 customer_id,
 SUM (amount)
FROM
 payment
GROUP BY
 customer_id;

The general pattern is that there is an aggregate function (here SUM) and that any column used in the GROUP BY has to appear in the result field list outside of the aggregate function.

So the typical design process is to discover the various (different) SQL queries that are necessary to get you the full range of search options that you require.

Then you decide how to use Ecto to compose the query under the various possible circumstances to arrive at the necessary query for the situation that matches the particular instance of search parameters (an example discussion about composing queries is here).

kip

kip

ex_cldr Core Team

The error is basically saying that every column in your select clause must also be either:

  1. An aggregate or
  2. Also included in the group by.

The reason is this: you are grouping values. if you have:

select bus_id, route_name group by bus_id

then somehow the database has to work out what to do with the route_name column. Return the first one? Last one? Average? you get the picture…

So you might (as an example only, not at all suggesting valid in your case):

select bus_id, min(route_name) group by bus_id

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
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 get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
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 127089 1222
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement