tanweerdev

tanweerdev

Dynamic having query help needed

Note: The query was not possible dynimally with ecto according to some of my search

I am trying to implement query in this post with dynamic AND+OR parts. I really couldnt think of how to do it with dynamic AND+OR parts. I can get the AND + OR list dynamically like this [[first_or_1, first_or_2, first_or_3], [second_or_1, second_or_2]]. The lists have to be joined. Here are schemas

defmodule MainTable do
  @moduledoc false

  use Web, :model

  schema "main_table" do
    field(:name, :string)
    many_to_many(:others, OtherTable, join_through: "bridge_table")
  end
end


defmodule OtherTable do
  @moduledoc false

  use Web, :model

  schema "other_table" do
    field(:name, :string)
    many_to_many(:mains, MainTable, join_through: "bridge_table")
  end
end

defmodule BridgeTable do
  @moduledoc false

  use Web, :model
  @derive Jason.Encoder

  schema "bridge_table" do
    belongs_to(:main, MainTable)
    belongs_to(:other, OtherTable)
  end
end

and here is simple join query

MainTable |> join(:inner, [m], bt in BridgeTable, on: bt.main_table_id == m.id) |> join(:inner, [m, ..., bt], ot in OtherTable, on: bt.other_table_id == ot.id)

I built my having part dynamically like this

"""? = COUNT( DISTINCT CASE WHEN ot.name = ANY ? THEN ot.name END )
OR ? = COUNT( DISTINCT CASE WHEN ot.name = ANY ?            THEN ot.name END ) """

and the params part I get like this. two lists

[["name 1", "name 2"], ["name 3"]]

Problem:: The above query will work if it gets a correct reference to otther_table ie ot table but ecto with create query with some other reference eg other_table as o1

I can also convert the above having part to

"""? = COUNT( DISTINCT CASE WHEN ? = ANY ? THEN ? END )
OR ? = COUNT( DISTINCT CASE WHEN ? = ANY ?            THEN ? END ) """

But how do I get/merge ot reference dynamically

Note: I created above sql like query so that I can use it with fragement as I dont find other way. and pass params eg

from q in query,
having: fragment(sql, params) // But how do I get reference of **ot** table here in params passed

Marked As Solved

LostKobrakai

LostKobrakai

condition_a = dynamic(…, …)
condition_b = dynamic(…, …)
condition_c = dynamic(…, …)
full_condition = dynamic(^condition_a or (^condition_b and ^condition_c))
query = from query, having: ^full_condition

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement