MYSQL how to calculate a percentage value with a join query












-1














I have two tables,



User_packages(id,customer_id,total,vat,government_tax )

Payments(package_id,amount)


One User_packages (Holiday package) can have many payments. Which means the total amount can be paid installment wise.



My goal is to display User_packages id and the percentage amount that the customer paid.
and the



full total = total + vat + government tax



User_packages



id | customer_id | total | vat | government_tax
1 | 1 | 2500 |100 | 50
2 | 2 | 4500 |100 | 50
3 | 1 | 5000 |200 |100


Payments



package_id | amount
1 | 500
1 | 2000
3 | 5000
3 | 300


MY EXPECTED RESULTS WOULD BE



package_id | payment percentage
1 | 94
2 | 0
3 | 100


How do I achieve this using MYSQL?
All I did up to now is the join query



SELECT User_packages.id,Payments.amount FROM booking_widget.User_packages 
left join Payments on User_packages.id = Payments.package_id;


I am using Server version: 5.7.24-0ubuntu0.18.04.1 (Ubuntu)










share|improve this question



























    -1














    I have two tables,



    User_packages(id,customer_id,total,vat,government_tax )

    Payments(package_id,amount)


    One User_packages (Holiday package) can have many payments. Which means the total amount can be paid installment wise.



    My goal is to display User_packages id and the percentage amount that the customer paid.
    and the



    full total = total + vat + government tax



    User_packages



    id | customer_id | total | vat | government_tax
    1 | 1 | 2500 |100 | 50
    2 | 2 | 4500 |100 | 50
    3 | 1 | 5000 |200 |100


    Payments



    package_id | amount
    1 | 500
    1 | 2000
    3 | 5000
    3 | 300


    MY EXPECTED RESULTS WOULD BE



    package_id | payment percentage
    1 | 94
    2 | 0
    3 | 100


    How do I achieve this using MYSQL?
    All I did up to now is the join query



    SELECT User_packages.id,Payments.amount FROM booking_widget.User_packages 
    left join Payments on User_packages.id = Payments.package_id;


    I am using Server version: 5.7.24-0ubuntu0.18.04.1 (Ubuntu)










    share|improve this question

























      -1












      -1








      -1







      I have two tables,



      User_packages(id,customer_id,total,vat,government_tax )

      Payments(package_id,amount)


      One User_packages (Holiday package) can have many payments. Which means the total amount can be paid installment wise.



      My goal is to display User_packages id and the percentage amount that the customer paid.
      and the



      full total = total + vat + government tax



      User_packages



      id | customer_id | total | vat | government_tax
      1 | 1 | 2500 |100 | 50
      2 | 2 | 4500 |100 | 50
      3 | 1 | 5000 |200 |100


      Payments



      package_id | amount
      1 | 500
      1 | 2000
      3 | 5000
      3 | 300


      MY EXPECTED RESULTS WOULD BE



      package_id | payment percentage
      1 | 94
      2 | 0
      3 | 100


      How do I achieve this using MYSQL?
      All I did up to now is the join query



      SELECT User_packages.id,Payments.amount FROM booking_widget.User_packages 
      left join Payments on User_packages.id = Payments.package_id;


      I am using Server version: 5.7.24-0ubuntu0.18.04.1 (Ubuntu)










      share|improve this question













      I have two tables,



      User_packages(id,customer_id,total,vat,government_tax )

      Payments(package_id,amount)


      One User_packages (Holiday package) can have many payments. Which means the total amount can be paid installment wise.



      My goal is to display User_packages id and the percentage amount that the customer paid.
      and the



      full total = total + vat + government tax



      User_packages



      id | customer_id | total | vat | government_tax
      1 | 1 | 2500 |100 | 50
      2 | 2 | 4500 |100 | 50
      3 | 1 | 5000 |200 |100


      Payments



      package_id | amount
      1 | 500
      1 | 2000
      3 | 5000
      3 | 300


      MY EXPECTED RESULTS WOULD BE



      package_id | payment percentage
      1 | 94
      2 | 0
      3 | 100


      How do I achieve this using MYSQL?
      All I did up to now is the join query



      SELECT User_packages.id,Payments.amount FROM booking_widget.User_packages 
      left join Payments on User_packages.id = Payments.package_id;


      I am using Server version: 5.7.24-0ubuntu0.18.04.1 (Ubuntu)







      mysql left-join






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      Pathum Samararathna

      708619




      708619
























          3 Answers
          3






          active

          oldest

          votes


















          1














          This query will give you the results you want:



          SELECT u.id, COALESCE(ROUND(p.amount / (u.total + u.vat + u.government_tax) * 100), 0) AS payment_percentage
          FROM user_packages u
          LEFT JOIN (SELECT package_id, SUM(amount) AS amount
          FROM payments
          GROUP BY package_id) p ON p.package_id = u.id


          Output:



          id  payment_percentage
          1 94
          2 0
          3 100


          Demo on dbfiddle






          share|improve this answer





















          • Thanks this works. If User_packages has a column called status if I want to filter a records with belongs to one status for example 'confirmed'. Is it okay to put the where clause en to this query? where u.status = 'confirmed'
            – Pathum Samararathna
            yesterday










          • Yes that should work fine.
            – Nick
            yesterday



















          1














          You can try below -



          SELECT User_packages.id,(coalesce(amt,0)*100.0)/(total+Vat+government_tax) as percentage
          FROM booking_widget.User_packages
          left join
          (select package_id,sum(amount) as amt from Payments group by package_id) as pay
          on User_packages.id = pay.package_id;





          share|improve this answer





















          • Thanks a lot. =)
            – Pathum Samararathna
            yesterday



















          0














          Try this:



          SELECT User_packages.id,(Amounts.total + User_packages.vat + User_packages.government_tax) as 'payment percentage'
          FROM booking_widget.User_packages
          JOIN (SELECT package_id, sum(amount) as total FROM Payments GROUP BY package_id) as Amounts
          ON User_packages.id = Amounts.package_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',
            autoActivateHeartbeat: false,
            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%2f53944294%2fmysql-how-to-calculate-a-percentage-value-with-a-join-query%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            This query will give you the results you want:



            SELECT u.id, COALESCE(ROUND(p.amount / (u.total + u.vat + u.government_tax) * 100), 0) AS payment_percentage
            FROM user_packages u
            LEFT JOIN (SELECT package_id, SUM(amount) AS amount
            FROM payments
            GROUP BY package_id) p ON p.package_id = u.id


            Output:



            id  payment_percentage
            1 94
            2 0
            3 100


            Demo on dbfiddle






            share|improve this answer





















            • Thanks this works. If User_packages has a column called status if I want to filter a records with belongs to one status for example 'confirmed'. Is it okay to put the where clause en to this query? where u.status = 'confirmed'
              – Pathum Samararathna
              yesterday










            • Yes that should work fine.
              – Nick
              yesterday
















            1














            This query will give you the results you want:



            SELECT u.id, COALESCE(ROUND(p.amount / (u.total + u.vat + u.government_tax) * 100), 0) AS payment_percentage
            FROM user_packages u
            LEFT JOIN (SELECT package_id, SUM(amount) AS amount
            FROM payments
            GROUP BY package_id) p ON p.package_id = u.id


            Output:



            id  payment_percentage
            1 94
            2 0
            3 100


            Demo on dbfiddle






            share|improve this answer





















            • Thanks this works. If User_packages has a column called status if I want to filter a records with belongs to one status for example 'confirmed'. Is it okay to put the where clause en to this query? where u.status = 'confirmed'
              – Pathum Samararathna
              yesterday










            • Yes that should work fine.
              – Nick
              yesterday














            1












            1








            1






            This query will give you the results you want:



            SELECT u.id, COALESCE(ROUND(p.amount / (u.total + u.vat + u.government_tax) * 100), 0) AS payment_percentage
            FROM user_packages u
            LEFT JOIN (SELECT package_id, SUM(amount) AS amount
            FROM payments
            GROUP BY package_id) p ON p.package_id = u.id


            Output:



            id  payment_percentage
            1 94
            2 0
            3 100


            Demo on dbfiddle






            share|improve this answer












            This query will give you the results you want:



            SELECT u.id, COALESCE(ROUND(p.amount / (u.total + u.vat + u.government_tax) * 100), 0) AS payment_percentage
            FROM user_packages u
            LEFT JOIN (SELECT package_id, SUM(amount) AS amount
            FROM payments
            GROUP BY package_id) p ON p.package_id = u.id


            Output:



            id  payment_percentage
            1 94
            2 0
            3 100


            Demo on dbfiddle







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered yesterday









            Nick

            23.4k91535




            23.4k91535












            • Thanks this works. If User_packages has a column called status if I want to filter a records with belongs to one status for example 'confirmed'. Is it okay to put the where clause en to this query? where u.status = 'confirmed'
              – Pathum Samararathna
              yesterday










            • Yes that should work fine.
              – Nick
              yesterday


















            • Thanks this works. If User_packages has a column called status if I want to filter a records with belongs to one status for example 'confirmed'. Is it okay to put the where clause en to this query? where u.status = 'confirmed'
              – Pathum Samararathna
              yesterday










            • Yes that should work fine.
              – Nick
              yesterday
















            Thanks this works. If User_packages has a column called status if I want to filter a records with belongs to one status for example 'confirmed'. Is it okay to put the where clause en to this query? where u.status = 'confirmed'
            – Pathum Samararathna
            yesterday




            Thanks this works. If User_packages has a column called status if I want to filter a records with belongs to one status for example 'confirmed'. Is it okay to put the where clause en to this query? where u.status = 'confirmed'
            – Pathum Samararathna
            yesterday












            Yes that should work fine.
            – Nick
            yesterday




            Yes that should work fine.
            – Nick
            yesterday













            1














            You can try below -



            SELECT User_packages.id,(coalesce(amt,0)*100.0)/(total+Vat+government_tax) as percentage
            FROM booking_widget.User_packages
            left join
            (select package_id,sum(amount) as amt from Payments group by package_id) as pay
            on User_packages.id = pay.package_id;





            share|improve this answer





















            • Thanks a lot. =)
              – Pathum Samararathna
              yesterday
















            1














            You can try below -



            SELECT User_packages.id,(coalesce(amt,0)*100.0)/(total+Vat+government_tax) as percentage
            FROM booking_widget.User_packages
            left join
            (select package_id,sum(amount) as amt from Payments group by package_id) as pay
            on User_packages.id = pay.package_id;





            share|improve this answer





















            • Thanks a lot. =)
              – Pathum Samararathna
              yesterday














            1












            1








            1






            You can try below -



            SELECT User_packages.id,(coalesce(amt,0)*100.0)/(total+Vat+government_tax) as percentage
            FROM booking_widget.User_packages
            left join
            (select package_id,sum(amount) as amt from Payments group by package_id) as pay
            on User_packages.id = pay.package_id;





            share|improve this answer












            You can try below -



            SELECT User_packages.id,(coalesce(amt,0)*100.0)/(total+Vat+government_tax) as percentage
            FROM booking_widget.User_packages
            left join
            (select package_id,sum(amount) as amt from Payments group by package_id) as pay
            on User_packages.id = pay.package_id;






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered yesterday









            fa06

            11k2917




            11k2917












            • Thanks a lot. =)
              – Pathum Samararathna
              yesterday


















            • Thanks a lot. =)
              – Pathum Samararathna
              yesterday
















            Thanks a lot. =)
            – Pathum Samararathna
            yesterday




            Thanks a lot. =)
            – Pathum Samararathna
            yesterday











            0














            Try this:



            SELECT User_packages.id,(Amounts.total + User_packages.vat + User_packages.government_tax) as 'payment percentage'
            FROM booking_widget.User_packages
            JOIN (SELECT package_id, sum(amount) as total FROM Payments GROUP BY package_id) as Amounts
            ON User_packages.id = Amounts.package_id;





            share|improve this answer


























              0














              Try this:



              SELECT User_packages.id,(Amounts.total + User_packages.vat + User_packages.government_tax) as 'payment percentage'
              FROM booking_widget.User_packages
              JOIN (SELECT package_id, sum(amount) as total FROM Payments GROUP BY package_id) as Amounts
              ON User_packages.id = Amounts.package_id;





              share|improve this answer
























                0












                0








                0






                Try this:



                SELECT User_packages.id,(Amounts.total + User_packages.vat + User_packages.government_tax) as 'payment percentage'
                FROM booking_widget.User_packages
                JOIN (SELECT package_id, sum(amount) as total FROM Payments GROUP BY package_id) as Amounts
                ON User_packages.id = Amounts.package_id;





                share|improve this answer












                Try this:



                SELECT User_packages.id,(Amounts.total + User_packages.vat + User_packages.government_tax) as 'payment percentage'
                FROM booking_widget.User_packages
                JOIN (SELECT package_id, sum(amount) as total FROM Payments GROUP BY package_id) as Amounts
                ON User_packages.id = Amounts.package_id;






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered yesterday









                WilsonPena

                4101415




                4101415






























                    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%2f53944294%2fmysql-how-to-calculate-a-percentage-value-with-a-join-query%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

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas