catra

catra

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

Showing Posts 1 to 2

gregvaughn

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))

catra

catra OP

@gregvaughn You are the best! Thank you!

— All posts loaded —

Where Next? Top

Trending in Chat/Questions Top

mhindujadheerajsudan
Hi, I’m Dheeraj Sudan from the UK. I’m a software developer and also run a business with my wife Meenu Hinduja. I’m interested in getting...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews