skip to Main Content

We have one table where there is a column player_id and another transaction_type (deposit || withdrawal)

We have one table where there is a column player_id and another transaction_type (deposit || withdrawal)

how to make a request to take the amount of the amount_local column and group it so that it is clear how many deposits there were and how many were withdrawn from one person and group it by player_id

2

Answers


  1. The transaction_type column can be used to distinguish between deposits and withdrawals, and the GROUP BY clause can be used to group the results by player_id. Here is an illustration SQL command for both tables:

    -- For the first table
    SELECT
        player_id,
        transaction_type,
        SUM(amount_local) AS total_amount
    FROM
        table1
    GROUP BY
        player_id,
        transaction_type;
    
    -- For the second table
    SELECT
        player_id,
        transaction_type,
        SUM(amount_local) AS total_amount
    FROM
        table2
    GROUP BY
        player_id,
        transaction_type;
    

    You will receive a return set from the above query that includes the columns player_id, transaction_type, and total_amount. Transaction_type will either be "deposit" or "withdrawal," and total_amount will be the total of the amounts for each player_id and transaction_type combination.

    Hope it works 🙂

    Login or Signup to reply.
  2. It’s really simple. You can use SQL’s SUM() function together with a CASE statement to discriminate between deposit and withdrawal transactions in order to retrieve the total deposit and withdrawal amounts aggregated by player_id. Here is a sample inquiry:

    SELECT
        player_id,
        SUM(CASE WHEN transaction_type = 'deposit' THEN amount_local ELSE 0 END) AS total_deposits,
        SUM(CASE WHEN transaction_type = 'withdrawal' THEN amount_local ELSE 0 END) AS total_withdrawals
    FROM your_table_name
    GROUP BY player_id;
    

    Your_table_name should be changed to the name of your table. The total deposit and withdrawal amounts for each player are determined by adding the values in the amount_local column for each player_id separately.

    Hope this helps, do let me know if you need more help.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search