Hello.
I didn’t run this snippet, but I think idea should work
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(...)