Do we have something similar in ecto?

hi. I have a query that is doing the sum of jsonb values and I’m using postgres 9.6. So my query looks like this

select t.id, sum(tt.value::text::numeric) val
    from my_table t, jsonb_each(total) AS tt
    where jsonb_typeof(tt.value) = 'number'
    group by t.id

I need to add this sum(tt.value::text::numeric) which I’m not sure how to do it in ecto?

Check json_extract_path
https://hexdocs.pm/ecto/Ecto.Query.API.html#json_extract_path/2

And combine with sum
https://hexdocs.pm/ecto/Ecto.Query.API.html#sum/1

or fragment
https://hexdocs.pm/ecto/Ecto.Query.API.html#fragment/1

2 Likes