How to write complex query with fragments
I'm trying to write this query with Ash
I've gotten this far but I'm getting an error about fragment/1 being undefined and also not sure how to implement the group by.
9 Replies
Well, you won’t be able to sort by
1
(not sure why you’d want to)
And you can’t select fragments
You’d need to add a calculation to the resource and select the calculation
Or load a one off calculation Ash.Query.calculate
I won’t be at a computer for a while but there are examples of expression calcs floating around and in the docsThe sort by 1 is to sort by the calculated result of the date, here is how I acommplished the query with ecto, maybe this is the better route for this case
make it a sql view?
🤔 does
order_by: 1
actually do that?
TIL about order_by: 1
just looked it up, apparently its a thing
What exactly are you looking to return?
Like thinking about it from a resource perspective, you'll be accessing this by making some specific query against a resource I imagine
The group_by: 1
will also be a problem. Remember the purpose of Ash is to sort of...regulate the way that you interact with these things, not necessarily to contain within it a correlary for every postgres operator.
One thing you can potentially do is:
In that query you might be able to accomplish what you want, although it might interrupt other features (like loading aggregates on the returned result)
Depending on exactly what you're looking to do, doing it with ecto might be the best way 🙂ya this wouldn't return any ash resource, it returns an avg coverage percentage by day
it is to build a graph
I’d probably put this as a calculation on a resource somewhere if possible, and then calculate it using ecto.
Would it make sense to create a virtual resource that isn't persisted to any datastore?
Yes, that would also be an option 🙂 it can add a bit of complexity depending on what features you want to support/how you model it
yeah you can use numbers for sort and order and group etc
I'm not a fan of it personally, prefer to be explicit and use names