How to get a similar result of db.query but with extra aggregation and using select
I have the following drizzle query which works correctly but the results is not what I expect since we are grouping votes as well by their ids.
I am trying to get
this results in all the votes which is what I am trying to achieve, but I am also trying to aggregate the total votes from the database as I believe it will be easier when filtering.
5 Replies
The objects for reference with db.select:
And this is with db.query:
You are trying to a lot in a single query
I think you need a window function for the count, and a json aggregation for the votes
It's doable but you have to check how to do it in sql and then translate to drizzle
This is probably what you need for the count: https://discord.com/channels/1043890932593987624/1164627420368875529/1164662014237487299
And this is how somebody is using json aggregation: https://discord.com/channels/1043890932593987624/1165959913252585492/1165988316018118746
I think I got it working, thanks the posts you have mentioned they were very helpful, I am not sure as you mentioned doing a lot in a single query I just did not want to do another trip to the database I believe it would be not ideal? I will be doing filtering but I want to do it in the database level as then when I implement pagination it will work properly.
Thanks again!
also how can I orderBy totalVotes when I order by feedbackId it seems to be working for some reason not sure, I was hoping I could orderBy totalVotes
You can pass a several parameters to orderBy
I think you'll need an alias to be able to reference the totalVotes column
I think I got it, thanks again!