Trying my hardest to reproduce this query with Ecto, but struggling:
SELECT DISTINCT("id"), sum("hits") AS hits
FROM "stats_daily"
WHERE ("day" = CURRENT_DATE - 1)
GROUP BY "id"
ORDER BY "hits"
DESC LIMIT 1000
I don’t believe I am meant to use an AS for the sum(“hits”) when using Ecto or I have not been able to find a working variant that uses as
If I use distinct, it becomes DISTINCT ON which is not what I want and it also modifies ORDER BY. I’ve tried to use fragments to work around that, but then ORDER BY is failing because “hits” doesn’t match.
the Ecto query before applying DISTINCT and SUM was like this:
Can you show the actual ecto query you ran as well as the logged SQL? A key part of my comment was “if you are already grouping”. The error seems to indicate that you aren’t. You need to do something like:
I had it stuck in my head that I needed DISTINCT because I saw this same change applied in another internal system accessing the same database which modified the query to have both DISTINCT and GROUP BY and the query is working over there.
I should not have just blindly trusted that this was the best solution, but I don’t write SQL all day and it seemed right.
Thank you kindly for your time.
edit: the use of |> order_by([p], desc: sum(p.hits)) ← sum(p.hits) here is really surprising to me as it looks like you’re trying to sum it here as well. I’ll have to research this more thoroughly.
@feld you have to sort by what is in the result of the query eg the select part. You aren’t selecting p.hits you’re selecting sum(p.hits) so that’s what you have to sort on as well. Postgres is smart enough to not do the sum twice.