How to order by foreign table's count?
# help
a
How to order by foreign table's count?
n
Hello @arcavid! This thread has been automatically created from your message in #843999948717555735 a few seconds ago. We have already mentioned the @User so that they can see your message and help you as soon as possible! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ``...`` menu) and select "Leave Thread" to unsubscribe from future updates. Want to change the title? Use the ``/title`` command! We have solved your problem? Click the button below to archive it.
g
Depends on where you want to do this. It can be done in Postgres sql functions, should be references thru Googling or asking in SQL section (I would have to look up solution). If you mean using an SB client, I've not seen a PostgREST solution, so you would have to post process by sorting your result based on the count you can obtain in the the resulting rows.
n
arcavid (2022-05-26)
a
Copy code
javascript
from('table_name')
.select(`id, comment(count)`)
.order('count', { ascending: false, foreignTable: 'comment' })
Shouldn't this work?
g
Does it? I don't think it would, as it probably would look for a column named count, BUT PostgREST is somewhat "tricky" with what it can do, especially with fk's and count. I've not found an example of anyone doing it it a short search. Looks like you also asked on github and Steve is in that thread and would be the authority on it, if he responds.
a
It doesn't, but I decided to have a count field to order by it. Thanks!