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’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










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