Limit select for each entry in "in"
Hey there I am trying to fetch the last message for severeal groups in one select.
which works fine but gives me back all of the messages of the group. I would like to only get one message for each group in the "in" condition. So if I got four groups with messages in it it should return four rows.
"limit" or "single" only work for the whole amount of rows so I am kind of stuck here.
6 Replies
Hi :vmathi: This is due to the fact that the
.limit()
function in Supabase applies to the entire result set, not individual entries in the IN
clause.
You could group the result by group
id, either by doing it with loadsh or something similar.Thanks for reaching out.
Sure I could solve this in the client but this would mean I would always fetch hundreds of messages although I only need one. I also thought about splitting it into a multiple selects and request them in parallel with Promise.all
Just thought there might be a solution I am missing. This should also be possible with a RPC right?
Definitely, this is what i would recommend you if you don't want to make any client side computation. Something like this should work;
And then you can call it like that:
Awesome I am gonna try this out. Thank you so much!
You're welcome :p
This solution worked fine but @David pointed out that limit also takes a foreignTable option to achieve this.
It would look something like this
In case anyone gets into a similar problem 🙂