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
Last Post!
catra
Popular in Chat/Questions
Background
I am trying to do the typical Ceaser cypher exercise in Elixir. The description of the exercise is as follows:
Create an impl...
New
Hello!,
I just started this week to discover Elixir.
I’m a PHP-Programmer and did some sutff in Go too.
The more I read about Elixir t...
New
hi i was wondering if it is necessary to learn erlang to learn elixir
New
A while back, I read a great book by Luis Atencio titled, Functional Programming in JavaScript. In section 7.3, he discusses memoization ...
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
Hello, I want to get started with elixir today. Having learnt the basics of Erlang (my first ever language) and seen what it’s can do, I’...
New
Hey, I have very restricted resources and time so I’m trying to understand the best way to learn Liveview in terms of cost/time.
The Pra...
New
Other popular topics
Hello, how can I check the Phoenix version ?
Thanks !
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
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something…
Haskell reminds me of Java, and e...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









