feld

feld

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:

    day = "day"

    from(p in "stats_daily", select: [p.id, p.hits], limit: ^count)
    |> where(fragment("? = CURRENT_DATE - 1", literal(^day)))
    |> order_by(desc: :hits)

Any tips would be greatly appreciated

Showing Posts 1 to 5

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

If you are already grouping by id, I don’t think distinct("id") is doing anything for you at all.

feld

feld OP

If I don’t I get SQL error like:

ERROR: column "stats_daily.id" must appear in the GROUP BY clause or be used in an aggregate function

edit: it seems to return the desired results if I keep the GROUP BY and remove the DISTINCT, so this gives me something new to work with

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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:

    day = "day"

    from(p in "stats_daily", select: [p.id, sum(p.hits)], limit: ^count)
    |> where(fragment("? = CURRENT_DATE - 1", literal(^day)))
    |> group_by([p], p.id)
    |> order_by([p], desc: sum(p.hits))
feld

feld OP

This worked.

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. :upside_down_face:

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.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

@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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
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
nseaSeb
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
RemyXRenard
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
velrest
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
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
FlyingNoodle
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

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews