Dynamically select a field using two tables

How can I dynamically select a field in this case?

I have two dates (sales.sales_date and cashier.cashier.date). Depending on my configuration I want to use sales_date or cachier_date in clauses where, select and group by;

result = list_sales_by_date(2, 2020, :cachier_date)
result = list_sales_by_date(2, 2020, :sales_date)


schema "sales" do
  field(:sales_date, :naive_datetime)
  field(:amount, :float)
  belongs_to(:cashier, Cashier,
      foreign_key: :cashier_id
    )
end

schema "cashier" do
  field(:cashier_date, :date)
  field(:is_opened, :boolean) 
end

def list_sales_by_date(month, year, date_field) do
  from(s in Sales)
  |> join(:inner, [s], c in Cashier, on: s.cashier_id == c.id)
  |> where(^do_where(date_field, year, month))
  |> select([s, c], %{
      year: extract_year(s.sales_date),
      month: extract_month(s.sales_date),
      amount: sum(s.amount),
      count_sales: count_distinct(s.id)
    })  
end

defp do_where(date_field, year, month) do
    if date_field == :sales_date do
      dynamic([s, c], extract_year(s.sales_date) == ^year and extract_month(s.sales_date) == ^month)
    else
      dynamic(
        [s, c],
        extract_year(c.cashier_date) == ^year and extract_month(c.cashier_date) == ^month
      )
    end
end 
1 Like

Hello.

I didn’t run this snippet, but I think idea should work :slight_smile:

query =
  from s in Sales,
    as: :sales,
    inner_join: c in Cashier,
    on: s.cashier_id == c.i,
    as: :cachier,
    select: %{
      count_sales: count_distinct(s.id),
      amount: sum(s.amount)
    }

{binding_name, field_name} =
  if date_field == :sales_date do
    {:sales, :sales_date}
  else
    {:cachier, :cachier_date}
  end

query
|> where([{^binding_name, table}],
    extract_year(field(table, ^field_name)) == ^year and extract_month(field(table, ^field_name)) == ^month
)
|> select_merge([{^binding_name, table}], %{
  year: extract_year(field(table, ^field_name)),
  month: extract_month(field(table, ^field_name)),
})
|> group_by(...)

Thanks, I was able to solve it using your idea