catra
Select by column
I have a Postgres view with 4 columns, two of them contains numbers. I want to create a function to get the sum of the elements depending on the column name.
This query is working well:
def sum_by_colum(column) do
Repo.aggregate(VwClientNodeBatteryStatus, :sum, column)
end
But I want to use something like:
from(vw in VwClientNodeBatteryStatus,
select: sum(vw.^column))
Because I can use Repo.one to get the popular {:ok, result} tuple. But I’m getting the following error:
`vw.^(column) is not a valid query expression. If you want to invoke vw.^/1 in a query, make sure that the module vw is required and that ^/1 is a macro
I guess Elixir/Ecto thinks that I have vw is a function. =S
Most Liked
gregvaughn
You’ll want to use field/2 (Ecto.Query.API — Ecto v3.14.0) to dynamically reference column names.
Untested code, but something like:
select sum(field(vw, ^column))
3
Popular in Chat/Questions
Anybody know of a Pragmatic Studio 40% off coupon code for video course like Phoenix?
New
Background
I have been reading quite a lot about design and architecture in Elixir and overall FP. My latest incursions took to me the On...
New
I’ve bought the following books:
Programming Elixir 1.6
Programming Phoenix 1.4
Programming Ecto
Functional Web Development with Elixir...
New
I’m not a hugely experienced programmer, just a few years. So I’m looking for resources to learn about a topic but I can’t seem to find m...
New
How to run the random query and get the result in JSON format and send it as a response. The query will be in the following form
DB Name...
New
Hello all,
I recently did my first app in Phoenix and Liveview, many thanks to all the users who assisted me. I found that the tutorial ...
New
I’d like to provide my review of the Elixir Course module from Groxio. I have some criticisms but I’d like to start with the positives.
...
New
I want to learn DSL. Don’t know how to write one. What;s the best introductory resource?
I see some macro being used here.
Is DSL only ...
New
Hello there,
i have a lot to read and to learn, but are there some books, that are focussing on how i “should” plan the architecture of ...
New
Hi all,
I am currently on this course
https://www.ludu.co/course/discover-elixir-phoenix
Half a way thru, and struggled a bit.. someti...
New
Other popular topics
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition)
It’s been a while since we first asked this, I...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New








