Calculate balances in transactions table where users send money to each other











up vote
1
down vote

favorite
1












I have a transaction table set up like this:



-- Transactions table
+----+---------+-------+------------------+--------+-----------+
| id | from_id | to_id | transaction_type | amount | card_type |
+----+---------+-------+------------------+--------+-----------+
| 1 | 1 | 1 | deposit | 90 | debit |
| 2 | 1 | 2 | transfer | -60 | debit |
| 3 | 2 | 2 | deposit | 10 | debit |
| 4 | 2 | 2 | deposit | 20 | credit |
+----+---------+-------+------------------+--------+-----------+


If i deposit it should show a positive value to show that money was added to my account, but if i do a transfer it should use a negative balance to show that money was removed from my account. The issue is, I can't think of a query that would add the money to the user 2 account from the transfer of user 1 to produce a view like this (based on card type):



-- Debit Balance Table
+---------+---------+
| user_id | balance |
+---------+---------+
| 1 | 30 |
| 2 | 70 |
+---------+---------+

-- Credit Balance Table
+---------+---------+
| user_id | balance |
+---------+---------+
| 1 | 0 |
| 2 | 20 |
+---------+---------+


I know you can't add money to a credit account but just forget that logic for now.










share|improve this question




























    up vote
    1
    down vote

    favorite
    1












    I have a transaction table set up like this:



    -- Transactions table
    +----+---------+-------+------------------+--------+-----------+
    | id | from_id | to_id | transaction_type | amount | card_type |
    +----+---------+-------+------------------+--------+-----------+
    | 1 | 1 | 1 | deposit | 90 | debit |
    | 2 | 1 | 2 | transfer | -60 | debit |
    | 3 | 2 | 2 | deposit | 10 | debit |
    | 4 | 2 | 2 | deposit | 20 | credit |
    +----+---------+-------+------------------+--------+-----------+


    If i deposit it should show a positive value to show that money was added to my account, but if i do a transfer it should use a negative balance to show that money was removed from my account. The issue is, I can't think of a query that would add the money to the user 2 account from the transfer of user 1 to produce a view like this (based on card type):



    -- Debit Balance Table
    +---------+---------+
    | user_id | balance |
    +---------+---------+
    | 1 | 30 |
    | 2 | 70 |
    +---------+---------+

    -- Credit Balance Table
    +---------+---------+
    | user_id | balance |
    +---------+---------+
    | 1 | 0 |
    | 2 | 20 |
    +---------+---------+


    I know you can't add money to a credit account but just forget that logic for now.










    share|improve this question


























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I have a transaction table set up like this:



      -- Transactions table
      +----+---------+-------+------------------+--------+-----------+
      | id | from_id | to_id | transaction_type | amount | card_type |
      +----+---------+-------+------------------+--------+-----------+
      | 1 | 1 | 1 | deposit | 90 | debit |
      | 2 | 1 | 2 | transfer | -60 | debit |
      | 3 | 2 | 2 | deposit | 10 | debit |
      | 4 | 2 | 2 | deposit | 20 | credit |
      +----+---------+-------+------------------+--------+-----------+


      If i deposit it should show a positive value to show that money was added to my account, but if i do a transfer it should use a negative balance to show that money was removed from my account. The issue is, I can't think of a query that would add the money to the user 2 account from the transfer of user 1 to produce a view like this (based on card type):



      -- Debit Balance Table
      +---------+---------+
      | user_id | balance |
      +---------+---------+
      | 1 | 30 |
      | 2 | 70 |
      +---------+---------+

      -- Credit Balance Table
      +---------+---------+
      | user_id | balance |
      +---------+---------+
      | 1 | 0 |
      | 2 | 20 |
      +---------+---------+


      I know you can't add money to a credit account but just forget that logic for now.










      share|improve this question















      I have a transaction table set up like this:



      -- Transactions table
      +----+---------+-------+------------------+--------+-----------+
      | id | from_id | to_id | transaction_type | amount | card_type |
      +----+---------+-------+------------------+--------+-----------+
      | 1 | 1 | 1 | deposit | 90 | debit |
      | 2 | 1 | 2 | transfer | -60 | debit |
      | 3 | 2 | 2 | deposit | 10 | debit |
      | 4 | 2 | 2 | deposit | 20 | credit |
      +----+---------+-------+------------------+--------+-----------+


      If i deposit it should show a positive value to show that money was added to my account, but if i do a transfer it should use a negative balance to show that money was removed from my account. The issue is, I can't think of a query that would add the money to the user 2 account from the transfer of user 1 to produce a view like this (based on card type):



      -- Debit Balance Table
      +---------+---------+
      | user_id | balance |
      +---------+---------+
      | 1 | 30 |
      | 2 | 70 |
      +---------+---------+

      -- Credit Balance Table
      +---------+---------+
      | user_id | balance |
      +---------+---------+
      | 1 | 0 |
      | 2 | 20 |
      +---------+---------+


      I know you can't add money to a credit account but just forget that logic for now.







      mysql sql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 11:09









      Strawberry

      25.6k83149




      25.6k83149










      asked Nov 8 at 11:05









      chinloyal

      36115




      36115
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          For debit, you can simply do conditional aggregation:



          SELECT 
          all_users.user_id,
          SUM (CASE
          WHEN t.transaction_type = 'deposit' AND all_users.user_id = t.from_id
          THEN ABS(t.amount)
          WHEN t.transaction_type = 'transfer' AND all_users.user_id = t.from_id
          THEN -ABS(t.amount)
          WHEN t.transaction_type = 'transfer' AND all_users.user_id = t.to_id
          THEN ABS(t.amount)
          ELSE 0
          END
          ) AS balance
          FROM transactions AS t
          JOIN (
          SELECT from_id AS user_id FROM transactions
          UNION
          SELECT to_id FROM transactions
          ) AS all_users
          ON t.from_id = all_users.user_id OR
          t.to_id = all_users.user_id
          WHERE t.card_type = 'debit'
          GROUP BY all_users.user_id





          share|improve this answer

















          • 1




            :) Gotta love stackoverflow, thank you.
            – chinloyal
            Nov 8 at 11:27










          • @chinloyal loved the question. bit tricky +1. Hope u understood what I am trying to do, else I can add explanation.
            – Madhur Bhaiya
            Nov 8 at 11:46






          • 1




            I'm not an expert at sql but I do understand what your doing, it's a very good solution. @MadhurBhaiya
            – chinloyal
            Nov 8 at 11:51











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206470%2fcalculate-balances-in-transactions-table-where-users-send-money-to-each-other%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          For debit, you can simply do conditional aggregation:



          SELECT 
          all_users.user_id,
          SUM (CASE
          WHEN t.transaction_type = 'deposit' AND all_users.user_id = t.from_id
          THEN ABS(t.amount)
          WHEN t.transaction_type = 'transfer' AND all_users.user_id = t.from_id
          THEN -ABS(t.amount)
          WHEN t.transaction_type = 'transfer' AND all_users.user_id = t.to_id
          THEN ABS(t.amount)
          ELSE 0
          END
          ) AS balance
          FROM transactions AS t
          JOIN (
          SELECT from_id AS user_id FROM transactions
          UNION
          SELECT to_id FROM transactions
          ) AS all_users
          ON t.from_id = all_users.user_id OR
          t.to_id = all_users.user_id
          WHERE t.card_type = 'debit'
          GROUP BY all_users.user_id





          share|improve this answer

















          • 1




            :) Gotta love stackoverflow, thank you.
            – chinloyal
            Nov 8 at 11:27










          • @chinloyal loved the question. bit tricky +1. Hope u understood what I am trying to do, else I can add explanation.
            – Madhur Bhaiya
            Nov 8 at 11:46






          • 1




            I'm not an expert at sql but I do understand what your doing, it's a very good solution. @MadhurBhaiya
            – chinloyal
            Nov 8 at 11:51















          up vote
          1
          down vote



          accepted










          For debit, you can simply do conditional aggregation:



          SELECT 
          all_users.user_id,
          SUM (CASE
          WHEN t.transaction_type = 'deposit' AND all_users.user_id = t.from_id
          THEN ABS(t.amount)
          WHEN t.transaction_type = 'transfer' AND all_users.user_id = t.from_id
          THEN -ABS(t.amount)
          WHEN t.transaction_type = 'transfer' AND all_users.user_id = t.to_id
          THEN ABS(t.amount)
          ELSE 0
          END
          ) AS balance
          FROM transactions AS t
          JOIN (
          SELECT from_id AS user_id FROM transactions
          UNION
          SELECT to_id FROM transactions
          ) AS all_users
          ON t.from_id = all_users.user_id OR
          t.to_id = all_users.user_id
          WHERE t.card_type = 'debit'
          GROUP BY all_users.user_id





          share|improve this answer

















          • 1




            :) Gotta love stackoverflow, thank you.
            – chinloyal
            Nov 8 at 11:27










          • @chinloyal loved the question. bit tricky +1. Hope u understood what I am trying to do, else I can add explanation.
            – Madhur Bhaiya
            Nov 8 at 11:46






          • 1




            I'm not an expert at sql but I do understand what your doing, it's a very good solution. @MadhurBhaiya
            – chinloyal
            Nov 8 at 11:51













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          For debit, you can simply do conditional aggregation:



          SELECT 
          all_users.user_id,
          SUM (CASE
          WHEN t.transaction_type = 'deposit' AND all_users.user_id = t.from_id
          THEN ABS(t.amount)
          WHEN t.transaction_type = 'transfer' AND all_users.user_id = t.from_id
          THEN -ABS(t.amount)
          WHEN t.transaction_type = 'transfer' AND all_users.user_id = t.to_id
          THEN ABS(t.amount)
          ELSE 0
          END
          ) AS balance
          FROM transactions AS t
          JOIN (
          SELECT from_id AS user_id FROM transactions
          UNION
          SELECT to_id FROM transactions
          ) AS all_users
          ON t.from_id = all_users.user_id OR
          t.to_id = all_users.user_id
          WHERE t.card_type = 'debit'
          GROUP BY all_users.user_id





          share|improve this answer












          For debit, you can simply do conditional aggregation:



          SELECT 
          all_users.user_id,
          SUM (CASE
          WHEN t.transaction_type = 'deposit' AND all_users.user_id = t.from_id
          THEN ABS(t.amount)
          WHEN t.transaction_type = 'transfer' AND all_users.user_id = t.from_id
          THEN -ABS(t.amount)
          WHEN t.transaction_type = 'transfer' AND all_users.user_id = t.to_id
          THEN ABS(t.amount)
          ELSE 0
          END
          ) AS balance
          FROM transactions AS t
          JOIN (
          SELECT from_id AS user_id FROM transactions
          UNION
          SELECT to_id FROM transactions
          ) AS all_users
          ON t.from_id = all_users.user_id OR
          t.to_id = all_users.user_id
          WHERE t.card_type = 'debit'
          GROUP BY all_users.user_id






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 11:15









          Madhur Bhaiya

          15.5k52136




          15.5k52136








          • 1




            :) Gotta love stackoverflow, thank you.
            – chinloyal
            Nov 8 at 11:27










          • @chinloyal loved the question. bit tricky +1. Hope u understood what I am trying to do, else I can add explanation.
            – Madhur Bhaiya
            Nov 8 at 11:46






          • 1




            I'm not an expert at sql but I do understand what your doing, it's a very good solution. @MadhurBhaiya
            – chinloyal
            Nov 8 at 11:51














          • 1




            :) Gotta love stackoverflow, thank you.
            – chinloyal
            Nov 8 at 11:27










          • @chinloyal loved the question. bit tricky +1. Hope u understood what I am trying to do, else I can add explanation.
            – Madhur Bhaiya
            Nov 8 at 11:46






          • 1




            I'm not an expert at sql but I do understand what your doing, it's a very good solution. @MadhurBhaiya
            – chinloyal
            Nov 8 at 11:51








          1




          1




          :) Gotta love stackoverflow, thank you.
          – chinloyal
          Nov 8 at 11:27




          :) Gotta love stackoverflow, thank you.
          – chinloyal
          Nov 8 at 11:27












          @chinloyal loved the question. bit tricky +1. Hope u understood what I am trying to do, else I can add explanation.
          – Madhur Bhaiya
          Nov 8 at 11:46




          @chinloyal loved the question. bit tricky +1. Hope u understood what I am trying to do, else I can add explanation.
          – Madhur Bhaiya
          Nov 8 at 11:46




          1




          1




          I'm not an expert at sql but I do understand what your doing, it's a very good solution. @MadhurBhaiya
          – chinloyal
          Nov 8 at 11:51




          I'm not an expert at sql but I do understand what your doing, it's a very good solution. @MadhurBhaiya
          – chinloyal
          Nov 8 at 11:51


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206470%2fcalculate-balances-in-transactions-table-where-users-send-money-to-each-other%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff