I have a legacy system that has a few queries similar to this one
SELECT
sum(CASE WHEN type_id = 1
THEN value END) AS inflow_total,
sum(CASE WHEN type_id = 2
THEN value END) AS outflow_total
FROM transaction
I’m trying to write the same app with elixir but I’m not sure on how to rewrite this using Ecto, so far I have this
def sum_of_inflow_and_outflow(query) do
from t in query, select: fragment("sum(CASE WHEN type_id = 1
THEN value END) AS inflow_total,
sum(CASE WHEN type_id = 2
THEN value END) AS outflow_total")
end
The result should I’m expecting is something like this.
When I run this query on the iex it simply returns 50
. Is there a better approach that does not involve sql fragments?