Ah thanks, I had tried using select_merge
before without luck, but your comment made me look at it again—here is what ended up working for me:
query = from(i in "data", select: %{})
fields = [:income_a, :income_b]
# fields = [:tax_a, :tax_b]
query =
Enum.reduce(fields, query, fn field, query ->
select_merge(query, [i], %{^Atom.to_string(field) => sum(field(i, ^field))})
end)