sergio

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

Showing Posts 1 to 5

sergio

sergio OP

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.

query =
      from b in Gaming.Stakes.Backer,
        left_join: wt in assoc(b, :wallet_transactions),
        left_join: pwt in assoc(b, :promotional_wallet_transactions),
        where: b.id == ^backer.id,
        where: wt.type == ^"winnings" or pwt.type == ^"winnings",
        group_by: b.id,
        select: %{
          backer_id: b.id,
          wallet_sums:
            fragment(
              """
              SUM( COALESCE( ?.amount,
              (
                'USD',
                0
              )
              :: money_with_currency ) )
              """,
              wt
            ),
          promotional_wallet_sums:
            fragment(
              """
              SUM( COALESCE( ?.amount,
              (
                'USD',
                0
              )
              :: money_with_currency ) )
              """,
              pwt
            )
        }

    query =
      from e in subquery(query),
        select: e.wallet_sums + e.promotional_wallet_sums

    Repo.one(query)
kip

kip

ex_cldr Core Team

Do I understand correctly that the intent is to sum :money_with_currency amounts, but treat NULL amounts as 0 amounts 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 of ex_money which it appears you may be using).

sergio

sergio OP

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

kip

ex_cldr Core Team

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:

Another bit of default behavior for a “strict” transition function is that the previous state value is retained unchanged whenever a null input value is encountered. Thus, null values are ignored. If you need some other behavior for null inputs, do not declare your transition function as strict; instead code it to test for null inputs and do whatever is needed.

And the current aggregate function is implemented as STRICT:

CREATE OR REPLACE FUNCTION money_state_function(agg_state money_with_currency, money money_with_currency)
RETURNS money_with_currency
IMMUTABLE
STRICT
LANGUAGE plpgsql
AS $$
  DECLARE
    expected_currency char(3);
    aggregate numeric;
    addition numeric;
  BEGIN
    if currency_code(agg_state) IS NULL then
      expected_currency := currency_code(money);
      aggregate := 0;
    else
      expected_currency := currency_code(agg_state);
      aggregate := amount(agg_state);
    end if;

    IF currency_code(money) = expected_currency THEN
      addition := aggregate + amount(money);
      return row(expected_currency, addition);
    ELSE
      RAISE EXCEPTION
        'Incompatible currency codes. Expected all currency codes to be %', expected_currency
        USING HINT = 'Please ensure all columns have the same currency code',
        ERRCODE = '22033';
    END IF;
  END;
$$;


CREATE AGGREGATE sum(money_with_currency)
(
  sfunc = money_state_function,
  stype = money_with_currency
);

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 STRICT tag from the function and handle NULL inputs directly so that part is clear. Postgres expects a NULL return 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 NULL values 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 NULL values 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 SUM in Ecto won’t be usable since two functions in the database with the same parameters are not possible. However a fragment should make that transparent (I can add a macro to do that).

Thoughts, suggestions very welcome.

LostKobrakai

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
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
achenet
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
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
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
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
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 Top

GenericJam
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
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
KristerV
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
mudasobwa
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews