MySQL multiple sums, using numbers from multiple tables









up vote
0
down vote

favorite












Let's say I have two tables, one storing accounts, one storing transactions :



id acct_name opening_bal
1 checking 1029.99
2 savings 2002.19
...


And



id date amount from_acct to_acct
...
99 2018-01-21 12.15 1 2
100 2018-01-21 9.99 4 1
101 2018-01-23 10.01 5 2
...


For example, row 99 in the transactions table is saying that 12.15 was transfered from checking to savings on 21 Jan 2018.



I would like to write a query that returns all accounts together with their balances on a given date (like today or 12 Oct 2018, etc), something like this :



acct balance
checking 1599.21
savings 2221.99
...


How would I write such a query?



Edit: Here's a solution, which is close enough to what I want (it just has an additional id column). You can replace CURDATE() with an arbitrary date to get the corresponding table of balances on that date.



SELECT id, acct_name, opening_bal+amt-amt2 as balance FROM accounts 
INNER JOIN
(SELECT to_acct, sum(amount) AS amt
FROM transactions
WHERE date <= CURDATE()
GROUP BY to_acct) as T2
ON accounts.id=T2.to_acct
INNER JOIN
(SELECT from_acct, sum(amount) AS amt2
FROM transactions
WHERE date <= CURDATE()
GROUP BY from_acct) as T3
ON T2.to_acct = T3.from_acct
;









share|improve this question























  • your question is pretty clear, but please add to the question what queries you have tried already, and in what way they did not work; that helps others know how much explanation they should provide with their answers, and helps others focus their answers on things you have not tried, and/or suggest adjustments to what you've already done
    – landru27
    Nov 11 at 19:18










  • Join the accounts table with the transactions table ON id IN (from_acct, to_acct). Add the opening balance to the sum of transfers in and the sum of the negative of transfers out.
    – Barmar
    Nov 11 at 19:57














up vote
0
down vote

favorite












Let's say I have two tables, one storing accounts, one storing transactions :



id acct_name opening_bal
1 checking 1029.99
2 savings 2002.19
...


And



id date amount from_acct to_acct
...
99 2018-01-21 12.15 1 2
100 2018-01-21 9.99 4 1
101 2018-01-23 10.01 5 2
...


For example, row 99 in the transactions table is saying that 12.15 was transfered from checking to savings on 21 Jan 2018.



I would like to write a query that returns all accounts together with their balances on a given date (like today or 12 Oct 2018, etc), something like this :



acct balance
checking 1599.21
savings 2221.99
...


How would I write such a query?



Edit: Here's a solution, which is close enough to what I want (it just has an additional id column). You can replace CURDATE() with an arbitrary date to get the corresponding table of balances on that date.



SELECT id, acct_name, opening_bal+amt-amt2 as balance FROM accounts 
INNER JOIN
(SELECT to_acct, sum(amount) AS amt
FROM transactions
WHERE date <= CURDATE()
GROUP BY to_acct) as T2
ON accounts.id=T2.to_acct
INNER JOIN
(SELECT from_acct, sum(amount) AS amt2
FROM transactions
WHERE date <= CURDATE()
GROUP BY from_acct) as T3
ON T2.to_acct = T3.from_acct
;









share|improve this question























  • your question is pretty clear, but please add to the question what queries you have tried already, and in what way they did not work; that helps others know how much explanation they should provide with their answers, and helps others focus their answers on things you have not tried, and/or suggest adjustments to what you've already done
    – landru27
    Nov 11 at 19:18










  • Join the accounts table with the transactions table ON id IN (from_acct, to_acct). Add the opening balance to the sum of transfers in and the sum of the negative of transfers out.
    – Barmar
    Nov 11 at 19:57












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Let's say I have two tables, one storing accounts, one storing transactions :



id acct_name opening_bal
1 checking 1029.99
2 savings 2002.19
...


And



id date amount from_acct to_acct
...
99 2018-01-21 12.15 1 2
100 2018-01-21 9.99 4 1
101 2018-01-23 10.01 5 2
...


For example, row 99 in the transactions table is saying that 12.15 was transfered from checking to savings on 21 Jan 2018.



I would like to write a query that returns all accounts together with their balances on a given date (like today or 12 Oct 2018, etc), something like this :



acct balance
checking 1599.21
savings 2221.99
...


How would I write such a query?



Edit: Here's a solution, which is close enough to what I want (it just has an additional id column). You can replace CURDATE() with an arbitrary date to get the corresponding table of balances on that date.



SELECT id, acct_name, opening_bal+amt-amt2 as balance FROM accounts 
INNER JOIN
(SELECT to_acct, sum(amount) AS amt
FROM transactions
WHERE date <= CURDATE()
GROUP BY to_acct) as T2
ON accounts.id=T2.to_acct
INNER JOIN
(SELECT from_acct, sum(amount) AS amt2
FROM transactions
WHERE date <= CURDATE()
GROUP BY from_acct) as T3
ON T2.to_acct = T3.from_acct
;









share|improve this question















Let's say I have two tables, one storing accounts, one storing transactions :



id acct_name opening_bal
1 checking 1029.99
2 savings 2002.19
...


And



id date amount from_acct to_acct
...
99 2018-01-21 12.15 1 2
100 2018-01-21 9.99 4 1
101 2018-01-23 10.01 5 2
...


For example, row 99 in the transactions table is saying that 12.15 was transfered from checking to savings on 21 Jan 2018.



I would like to write a query that returns all accounts together with their balances on a given date (like today or 12 Oct 2018, etc), something like this :



acct balance
checking 1599.21
savings 2221.99
...


How would I write such a query?



Edit: Here's a solution, which is close enough to what I want (it just has an additional id column). You can replace CURDATE() with an arbitrary date to get the corresponding table of balances on that date.



SELECT id, acct_name, opening_bal+amt-amt2 as balance FROM accounts 
INNER JOIN
(SELECT to_acct, sum(amount) AS amt
FROM transactions
WHERE date <= CURDATE()
GROUP BY to_acct) as T2
ON accounts.id=T2.to_acct
INNER JOIN
(SELECT from_acct, sum(amount) AS amt2
FROM transactions
WHERE date <= CURDATE()
GROUP BY from_acct) as T3
ON T2.to_acct = T3.from_acct
;






mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 20:26

























asked Nov 11 at 19:06









vgty6h7uij

1034




1034











  • your question is pretty clear, but please add to the question what queries you have tried already, and in what way they did not work; that helps others know how much explanation they should provide with their answers, and helps others focus their answers on things you have not tried, and/or suggest adjustments to what you've already done
    – landru27
    Nov 11 at 19:18










  • Join the accounts table with the transactions table ON id IN (from_acct, to_acct). Add the opening balance to the sum of transfers in and the sum of the negative of transfers out.
    – Barmar
    Nov 11 at 19:57
















  • your question is pretty clear, but please add to the question what queries you have tried already, and in what way they did not work; that helps others know how much explanation they should provide with their answers, and helps others focus their answers on things you have not tried, and/or suggest adjustments to what you've already done
    – landru27
    Nov 11 at 19:18










  • Join the accounts table with the transactions table ON id IN (from_acct, to_acct). Add the opening balance to the sum of transfers in and the sum of the negative of transfers out.
    – Barmar
    Nov 11 at 19:57















your question is pretty clear, but please add to the question what queries you have tried already, and in what way they did not work; that helps others know how much explanation they should provide with their answers, and helps others focus their answers on things you have not tried, and/or suggest adjustments to what you've already done
– landru27
Nov 11 at 19:18




your question is pretty clear, but please add to the question what queries you have tried already, and in what way they did not work; that helps others know how much explanation they should provide with their answers, and helps others focus their answers on things you have not tried, and/or suggest adjustments to what you've already done
– landru27
Nov 11 at 19:18












Join the accounts table with the transactions table ON id IN (from_acct, to_acct). Add the opening balance to the sum of transfers in and the sum of the negative of transfers out.
– Barmar
Nov 11 at 19:57




Join the accounts table with the transactions table ON id IN (from_acct, to_acct). Add the opening balance to the sum of transfers in and the sum of the negative of transfers out.
– Barmar
Nov 11 at 19:57












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Something like this. If you'd provided more data, answer could be more precise. Table1 is your first table, Table2 is the second.



select acct_name as acct,
opening_bal - t1.put_money + t2.get_money as balance
from Table1
left join (select from_acct, ifnull(sum(amount),0) as put_money from Table2 group by from_acct) t1
on t1.from_acct = Table1.id
left join (select to_acct, ifnull(sum(amount),0) as get_money from Table2 group by to_acct) t2
on t2.to_acct = Table1.id;





share|improve this answer






















    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%2f53252182%2fmysql-multiple-sums-using-numbers-from-multiple-tables%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










    Something like this. If you'd provided more data, answer could be more precise. Table1 is your first table, Table2 is the second.



    select acct_name as acct,
    opening_bal - t1.put_money + t2.get_money as balance
    from Table1
    left join (select from_acct, ifnull(sum(amount),0) as put_money from Table2 group by from_acct) t1
    on t1.from_acct = Table1.id
    left join (select to_acct, ifnull(sum(amount),0) as get_money from Table2 group by to_acct) t2
    on t2.to_acct = Table1.id;





    share|improve this answer


























      up vote
      1
      down vote



      accepted










      Something like this. If you'd provided more data, answer could be more precise. Table1 is your first table, Table2 is the second.



      select acct_name as acct,
      opening_bal - t1.put_money + t2.get_money as balance
      from Table1
      left join (select from_acct, ifnull(sum(amount),0) as put_money from Table2 group by from_acct) t1
      on t1.from_acct = Table1.id
      left join (select to_acct, ifnull(sum(amount),0) as get_money from Table2 group by to_acct) t2
      on t2.to_acct = Table1.id;





      share|improve this answer
























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Something like this. If you'd provided more data, answer could be more precise. Table1 is your first table, Table2 is the second.



        select acct_name as acct,
        opening_bal - t1.put_money + t2.get_money as balance
        from Table1
        left join (select from_acct, ifnull(sum(amount),0) as put_money from Table2 group by from_acct) t1
        on t1.from_acct = Table1.id
        left join (select to_acct, ifnull(sum(amount),0) as get_money from Table2 group by to_acct) t2
        on t2.to_acct = Table1.id;





        share|improve this answer














        Something like this. If you'd provided more data, answer could be more precise. Table1 is your first table, Table2 is the second.



        select acct_name as acct,
        opening_bal - t1.put_money + t2.get_money as balance
        from Table1
        left join (select from_acct, ifnull(sum(amount),0) as put_money from Table2 group by from_acct) t1
        on t1.from_acct = Table1.id
        left join (select to_acct, ifnull(sum(amount),0) as get_money from Table2 group by to_acct) t2
        on t2.to_acct = Table1.id;






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 20:05

























        answered Nov 11 at 19:59









        Michael O.

        2,6992521




        2,6992521



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252182%2fmysql-multiple-sums-using-numbers-from-multiple-tables%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







            這個網誌中的熱門文章

            What does pagestruct do in Eviews?

            Dutch intervention in Lombok and Karangasem

            Channel Islands