acrolink
I have this SQL query:
SELECT DISTINCT ON (b.id) b.id as book_id, r.id as record_id, r.due_for_return, r.returned_at
FROM books b
left join records r
on b.id = r.book_id
order by b.id, r.id DESC
Any idea how to convert it to an Ecto Query, especially the DISTINCT ON part ? Thank you.
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Other Trending Topics
Backend Developer for Elixir (F/M/D) - Shape the Future of Sports Photography with Us!(German Version below)
About us
We are athletes –...
New
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
acrolink
Maybe part of the solution:
Any comments are still welcome
acrolink
I have just noticed that the above query does not return the expected results. I was to select from
recordsthe row with the highestid(newest in database) only for eachbook_id. Seems that usingDISTINCTdoes not answer this need.acrolink
I guess for what I am trying to do I need to run a query with a sub query: first select max id with needed fields (grouped by book_id) from b and after that join a with b.
joaquinalcerro
Have you tried composing queries step by step:
acrolink
Some issues were related to the fact that in
Postgres, when usingDISTINCT ON, that column must appear first in theORDER BY. Anyway, the workingSQLfor what I need is this:How to write this in
Ecto, God knows and the brilliant folks here :).peerreynders
How to use PostgreSQL’s DISTINCT ON in Ecto Query
PostgreSQL GROUP BY
acrolink
@peerreynders, Words cannot express how grateful I am to you. Despite that I did not express clearly what I was trying to achieve, you have grasped it and provided the needed solution. In the meantime, I had written a working code, like this:
But your code looks nicer and possibly faster than the above.
I have learned much about
SQLandEctowith this, yet at the same time I come to question the design of my tables. Would the above approach work fast when there are say 100,000 records in the database? Would it not be better to have a[unique_constraint: (book_id, status:1)field in the records table indicating the active (or most recent row per book_id) and not to look for it by max(id).peerreynders
Is it slow now?
I don’t know anything about PostgreSQL’s performance characteristics. And ultimately you would have to benchmark the query on your configuration.
There may be cases where an
activecolumn can be the best solution but there is the trade off of the required additional update the needs to happen within the same transaction as the insert (both of which affect the index).Another alternative is to formulate the query around
due_for_returnand put an index on that but I don’t think it’s going to perform any better than using the primary key index.acrolink
No, it is not slow. It would be interesting to do some benchmarks when the data gets bigger and vs. having an
activefield in records. I have generated some dummy ~5000 books and ~60,000 records, results: 68ms to count them and 134ms to return a set of 20 records with limit and offset.peerreynders
You could also move the query into a view. That way the Ecto
selectis extremely simple and you can change the query inside the database if needed later.PostgreSQL Views