cjbottaro
I know this has been asked before, but I still can’t get it to work. I have code like this…
def case_stmt(input) do
[c1, c2, c3, c4] = generate_clauses(input)
Ecto.Query.dynamic([foo: a, bar: b], fragment("""
CASE WHEN ? THEN 1 ELSE 0 END +
CASE WHEN ? THEN 1 ELSE 0 END +
CASE WHEN ? THEN 1 ELSE 0 END +
CASE WHEN ? THEN 1 ELSE 0 END
""", ^c1, ^c2, ^c3, ^c4))
end
Which works fine when the number of generated clauses is exactly 4. What about when it’s an unknown amount? I want to build up the SQL string using reduce.
There is a really good blog post about making SQL case statements with Ecto:
The problem is that it doesn’t work when you pass it a variable; it only works when you pass a literal.
Any ideas on how to make this work? My use case is user defined queries that are stored in a database. All the user input is validated before making the db records of course. “Just make a case clause for each field the user can search on.” doesn’t work because the schema itself is user defined.
Thanks for the help!
Trending in Questions
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
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
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
fuelen
The implementation of the macro uses fragments which must be defined at compile time, not at runtime. You cannot pass a list of when/then clauses.
Could you show what exactly do you want to run? How the data is stored and what SQL do you want to build? Maybe we can figure out a different algorithm
fuelen
Tip: how would you implement dynamic
condin Elixir? You cannot pass conditions dynamically as well.dimitarvp
Why not separate them and do the summing in Elixir? You can also just use
select_mergewith dummy map keys and then use the resulting map’s values and sum that, similarly to this thread: Aggregate multiple dynamically selected columns in Ecto (without group by) - #3 by zackmichenerLostKobrakai
This might help with variable number of elements, where you don‘t know the number of elements at compile time. Not sure if it works with CASE though.
dimitarvp
I realize that, yeah. Shame we don’t use a placeholder repo for such forum interactions. Somebody should get to that one day.
cjbottaro
Figured it out after chasing down those ideas, but ultimately went a different route (I think).
I need it done in SQL because I’m going to use that count in a where clause.
Very cool! I had been knee deep in Ecto docs for the past couple of days and still managed to miss that. Going to have to tuck that away in my head for later use. I tried using it here with a like a
fragment("sum(?)", splice(clauses))but it didn’t work cuz that’s not how sql sum works…dimitarvp
Oh that’s pretty clever. Nice job!