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

jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement