sergio
I’m trying to find the total amount of winnings for a specific backer in my database.
query =
from b in Gaming.Stakes.Backer,
left_join: bwt in assoc(b, :wallet_transactions),
left_join: bpwt in assoc(b, :backer_promotional_wallet_transactions),
where: b.id == ^backer.id,
where: bwt.type == "winnings" or bpwt.type == "winnings",
group_by: b.id
from e in subquery(query),
select: [e.backer_id, e.wallet_sums + e.promotional_wallet_sums]
I’m a bit stuck on how to do the coalesce, and the nested select. Ultimately what I need a single value to come back from the database.
This is the raw SQL query that works as intended and give me the data I need. I’m trying to get this into Ecto code.
SELECT
backer_id,
wallet_sums + promotional_wallet_sums AS total
FROM
(
SELECT
backers.id AS backer_id,
SUM( COALESCE( wallet_transactions.amount,
(
'USD',
0
)
:: money_with_currency ) ) AS wallet_sums,
SUM( COALESCE( promotional_wallet_transactions.amount,
(
'USD',
0
)
:: money_with_currency ) ) AS promotional_wallet_sums
FROM
backers
LEFT JOIN
backer_wallet_transactions
ON backer_wallet_transactions.backer_id = backers.id
LEFT JOIN
wallet_transactions
ON wallet_transactions.id = backer_wallet_transactions.wallet_transaction_id
LEFT JOIN
backer_promotional_wallet_transactions
ON backer_promotional_wallet_transactions.backer_id = backers.id
LEFT JOIN
promotional_wallet_transactions
ON promotional_wallet_transactions.id = baker_promotional_wallet_transactions.promotional_wallet_transaction_id
WHERE
backers.id = 'c9ffd955-3333-3333-3333-578346b33333'
AND
(
wallet_transactions.type = 'winnings'
OR promotional_wallet_transactions.type = 'winnings'
)
GROUP BY
backers.id
)
sums
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #ai
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sergio
I managed to figure it out.
The tricky part was to use the
?fragment to reference the relationship because of it’s indirect in sql -vs- direct in ecto relationship magic nature.kip
Do I understand correctly that the intent is to sum
:money_with_currencyamounts, but treatNULLamounts as0amounts in the currently aggregating currency (USD for your example?). That might be possible and reasonable to implement as an additional aggregate function and I’ll take a look (I’m the author ofex_moneywhich it appears you may be using).sergio
Yes you are correct that would be awesome thank you for all your work on this package. we would be in quite the pickle without ex_money!
kip
I’ve been revisiting this topic (sorry for the delay). I have opened an issue to track requirements and issues for implementation.
The Postgres documentation says:
And the current aggregate function is implemented as
STRICT:I recall that @LostKobrakai had some very good input when I was originally implementing this function so perhaps he will have a point of view.
Basically I can remove the
STRICTtag from the function and handleNULLinputs directly so that part is clear. Postgres expects aNULLreturn for an aggregate if there are now non-NULL values and I can manage that too.What I’m not sure about is whether overall this is the right thing to do. Treating
NULLvalues as zero money amounts is probably not what most developers would expect.Therefore I think the right approach is to implement a separate aggregate function that very explicitly treats
NULLvalues as zero-amount money values. It will not only be explicit, but it won’t risk changing the behaviour unexpectantly for current users.The downside is that it means the built-in support for
SUMin Ecto won’t be usable since two functions in the database with the same parameters are not possible. However afragmentshould make that transparent (I can add a macro to do that).Thoughts, suggestions very welcome.
LostKobrakai
In my opinion sum for money should work exactly as sum works on numeric/decimal columns would, granted all currencies are the same. Any different behavior should be a custom and separately documented aggregation function.