Getting aggregate sub-values in aggregate query












0















I have a query that need to show aggregated values in an already aggregated outer query.



Consider a query that fetches total count of accounts being credited on a certain day.



I would like to display




  1. total number orders

  2. Total orders that are "PENDING"

  3. Total orders that are "OPEN"


Here is the query that seems to make sense but as resultset show below, I am obviously not using the right approach. The "OPEN" column displays the entire total, the "PENDING" column stays NULL.



I feel I may be using the wrong approach, what are the options to have this work?



SELECT s.created, COUNT(s.id) 'total_accounts_credited', SUM(s.withdrawal) 'total_amount_credited', 
(select count(s.id) WHERE s.status_id = 'OPEN') total_open_credited,
(select count(s.id) WHERE s.status_id = 'PENDING') total_pending_credited
FROM statements s
WHERE s.status_id in ('OPEN', 'PENDING')
GROUP BY
YEAR(s.created),
MONTH(s.created),
DAY(s.created)
ORDER BY s.created DESC
;


Results:



+---------------------+-------------------------+-----------------------+---------------------+----------------------+
| created | total_accounts_credited | total_amount_credited | total_open_credited | total_pending_credited |
+---------------------+-------------------------+-----------------------+---------------------+----------------------+
| 2019-01-19 00:00:00 | 18050 | 20813.18 | 18050 | NULL |
| 2019-01-12 00:00:00 | 18135 | 24768.43 | 18135 | NULL |
| 2019-01-10 09:00:27 | 80 | 1497.75 | 80 | NULL |
| 2019-01-09 09:20:55 | 51 | 933.50 | 51 | NULL |
| 2019-01-08 16:45:14 | 10 | 187.50 | 10 | NULL |
| 2019-01-05 18:21:00 | 17588 | 16968.49 | 17588 | NULL |
| 2018-12-29 00:00:00 | 17893 | 25404.18 | 17893 | NULL |
| 2018-12-28 15:23:04 | 1 | 35.00 | 1 | NULL |
| 2018-12-22 00:00:00 | 17353 | 17048.18 | 17353 | NULL |
| 2018-12-15 00:00:00 | 16893 | 10181.34 | 16893 | NULL |
| 2018-12-08 00:00:00 | 16220 | 99547.09 | 16220 | NULL |
| 2018-12-01 00:00:00 | 15476 | 87699.59 | 15476 | NULL |
+---------------------+-------------------------+-----------------------+---------------------+----------------------+









share|improve this question



























    0















    I have a query that need to show aggregated values in an already aggregated outer query.



    Consider a query that fetches total count of accounts being credited on a certain day.



    I would like to display




    1. total number orders

    2. Total orders that are "PENDING"

    3. Total orders that are "OPEN"


    Here is the query that seems to make sense but as resultset show below, I am obviously not using the right approach. The "OPEN" column displays the entire total, the "PENDING" column stays NULL.



    I feel I may be using the wrong approach, what are the options to have this work?



    SELECT s.created, COUNT(s.id) 'total_accounts_credited', SUM(s.withdrawal) 'total_amount_credited', 
    (select count(s.id) WHERE s.status_id = 'OPEN') total_open_credited,
    (select count(s.id) WHERE s.status_id = 'PENDING') total_pending_credited
    FROM statements s
    WHERE s.status_id in ('OPEN', 'PENDING')
    GROUP BY
    YEAR(s.created),
    MONTH(s.created),
    DAY(s.created)
    ORDER BY s.created DESC
    ;


    Results:



    +---------------------+-------------------------+-----------------------+---------------------+----------------------+
    | created | total_accounts_credited | total_amount_credited | total_open_credited | total_pending_credited |
    +---------------------+-------------------------+-----------------------+---------------------+----------------------+
    | 2019-01-19 00:00:00 | 18050 | 20813.18 | 18050 | NULL |
    | 2019-01-12 00:00:00 | 18135 | 24768.43 | 18135 | NULL |
    | 2019-01-10 09:00:27 | 80 | 1497.75 | 80 | NULL |
    | 2019-01-09 09:20:55 | 51 | 933.50 | 51 | NULL |
    | 2019-01-08 16:45:14 | 10 | 187.50 | 10 | NULL |
    | 2019-01-05 18:21:00 | 17588 | 16968.49 | 17588 | NULL |
    | 2018-12-29 00:00:00 | 17893 | 25404.18 | 17893 | NULL |
    | 2018-12-28 15:23:04 | 1 | 35.00 | 1 | NULL |
    | 2018-12-22 00:00:00 | 17353 | 17048.18 | 17353 | NULL |
    | 2018-12-15 00:00:00 | 16893 | 10181.34 | 16893 | NULL |
    | 2018-12-08 00:00:00 | 16220 | 99547.09 | 16220 | NULL |
    | 2018-12-01 00:00:00 | 15476 | 87699.59 | 15476 | NULL |
    +---------------------+-------------------------+-----------------------+---------------------+----------------------+









    share|improve this question

























      0












      0








      0








      I have a query that need to show aggregated values in an already aggregated outer query.



      Consider a query that fetches total count of accounts being credited on a certain day.



      I would like to display




      1. total number orders

      2. Total orders that are "PENDING"

      3. Total orders that are "OPEN"


      Here is the query that seems to make sense but as resultset show below, I am obviously not using the right approach. The "OPEN" column displays the entire total, the "PENDING" column stays NULL.



      I feel I may be using the wrong approach, what are the options to have this work?



      SELECT s.created, COUNT(s.id) 'total_accounts_credited', SUM(s.withdrawal) 'total_amount_credited', 
      (select count(s.id) WHERE s.status_id = 'OPEN') total_open_credited,
      (select count(s.id) WHERE s.status_id = 'PENDING') total_pending_credited
      FROM statements s
      WHERE s.status_id in ('OPEN', 'PENDING')
      GROUP BY
      YEAR(s.created),
      MONTH(s.created),
      DAY(s.created)
      ORDER BY s.created DESC
      ;


      Results:



      +---------------------+-------------------------+-----------------------+---------------------+----------------------+
      | created | total_accounts_credited | total_amount_credited | total_open_credited | total_pending_credited |
      +---------------------+-------------------------+-----------------------+---------------------+----------------------+
      | 2019-01-19 00:00:00 | 18050 | 20813.18 | 18050 | NULL |
      | 2019-01-12 00:00:00 | 18135 | 24768.43 | 18135 | NULL |
      | 2019-01-10 09:00:27 | 80 | 1497.75 | 80 | NULL |
      | 2019-01-09 09:20:55 | 51 | 933.50 | 51 | NULL |
      | 2019-01-08 16:45:14 | 10 | 187.50 | 10 | NULL |
      | 2019-01-05 18:21:00 | 17588 | 16968.49 | 17588 | NULL |
      | 2018-12-29 00:00:00 | 17893 | 25404.18 | 17893 | NULL |
      | 2018-12-28 15:23:04 | 1 | 35.00 | 1 | NULL |
      | 2018-12-22 00:00:00 | 17353 | 17048.18 | 17353 | NULL |
      | 2018-12-15 00:00:00 | 16893 | 10181.34 | 16893 | NULL |
      | 2018-12-08 00:00:00 | 16220 | 99547.09 | 16220 | NULL |
      | 2018-12-01 00:00:00 | 15476 | 87699.59 | 15476 | NULL |
      +---------------------+-------------------------+-----------------------+---------------------+----------------------+









      share|improve this question














      I have a query that need to show aggregated values in an already aggregated outer query.



      Consider a query that fetches total count of accounts being credited on a certain day.



      I would like to display




      1. total number orders

      2. Total orders that are "PENDING"

      3. Total orders that are "OPEN"


      Here is the query that seems to make sense but as resultset show below, I am obviously not using the right approach. The "OPEN" column displays the entire total, the "PENDING" column stays NULL.



      I feel I may be using the wrong approach, what are the options to have this work?



      SELECT s.created, COUNT(s.id) 'total_accounts_credited', SUM(s.withdrawal) 'total_amount_credited', 
      (select count(s.id) WHERE s.status_id = 'OPEN') total_open_credited,
      (select count(s.id) WHERE s.status_id = 'PENDING') total_pending_credited
      FROM statements s
      WHERE s.status_id in ('OPEN', 'PENDING')
      GROUP BY
      YEAR(s.created),
      MONTH(s.created),
      DAY(s.created)
      ORDER BY s.created DESC
      ;


      Results:



      +---------------------+-------------------------+-----------------------+---------------------+----------------------+
      | created | total_accounts_credited | total_amount_credited | total_open_credited | total_pending_credited |
      +---------------------+-------------------------+-----------------------+---------------------+----------------------+
      | 2019-01-19 00:00:00 | 18050 | 20813.18 | 18050 | NULL |
      | 2019-01-12 00:00:00 | 18135 | 24768.43 | 18135 | NULL |
      | 2019-01-10 09:00:27 | 80 | 1497.75 | 80 | NULL |
      | 2019-01-09 09:20:55 | 51 | 933.50 | 51 | NULL |
      | 2019-01-08 16:45:14 | 10 | 187.50 | 10 | NULL |
      | 2019-01-05 18:21:00 | 17588 | 16968.49 | 17588 | NULL |
      | 2018-12-29 00:00:00 | 17893 | 25404.18 | 17893 | NULL |
      | 2018-12-28 15:23:04 | 1 | 35.00 | 1 | NULL |
      | 2018-12-22 00:00:00 | 17353 | 17048.18 | 17353 | NULL |
      | 2018-12-15 00:00:00 | 16893 | 10181.34 | 16893 | NULL |
      | 2018-12-08 00:00:00 | 16220 | 99547.09 | 16220 | NULL |
      | 2018-12-01 00:00:00 | 15476 | 87699.59 | 15476 | NULL |
      +---------------------+-------------------------+-----------------------+---------------------+----------------------+






      mysql mysql-5.6 query subquery






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 5 hours ago









      stefgosselinstefgosselin

      1043




      1043






















          1 Answer
          1






          active

          oldest

          votes


















          0














          SELECT  ...,
          SUM(s.status_id = 'OPEN') AS total_open_credited,
          ...
          FROM ...
          WHERE ...
          GROUP BY DATE(s.created) -- a simplification
          ORDER BY ...


          Explanation: s.status_id = 'OPEN' is a boolean expression that evaluates to TRUE or FALSE. TRUE is 1; FALSE is 0. Then SUM correctly tallies what you asked for.






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "182"
            };
            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: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2fdba.stackexchange.com%2fquestions%2f227576%2fgetting-aggregate-sub-values-in-aggregate-query%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









            0














            SELECT  ...,
            SUM(s.status_id = 'OPEN') AS total_open_credited,
            ...
            FROM ...
            WHERE ...
            GROUP BY DATE(s.created) -- a simplification
            ORDER BY ...


            Explanation: s.status_id = 'OPEN' is a boolean expression that evaluates to TRUE or FALSE. TRUE is 1; FALSE is 0. Then SUM correctly tallies what you asked for.






            share|improve this answer




























              0














              SELECT  ...,
              SUM(s.status_id = 'OPEN') AS total_open_credited,
              ...
              FROM ...
              WHERE ...
              GROUP BY DATE(s.created) -- a simplification
              ORDER BY ...


              Explanation: s.status_id = 'OPEN' is a boolean expression that evaluates to TRUE or FALSE. TRUE is 1; FALSE is 0. Then SUM correctly tallies what you asked for.






              share|improve this answer


























                0












                0








                0







                SELECT  ...,
                SUM(s.status_id = 'OPEN') AS total_open_credited,
                ...
                FROM ...
                WHERE ...
                GROUP BY DATE(s.created) -- a simplification
                ORDER BY ...


                Explanation: s.status_id = 'OPEN' is a boolean expression that evaluates to TRUE or FALSE. TRUE is 1; FALSE is 0. Then SUM correctly tallies what you asked for.






                share|improve this answer













                SELECT  ...,
                SUM(s.status_id = 'OPEN') AS total_open_credited,
                ...
                FROM ...
                WHERE ...
                GROUP BY DATE(s.created) -- a simplification
                ORDER BY ...


                Explanation: s.status_id = 'OPEN' is a boolean expression that evaluates to TRUE or FALSE. TRUE is 1; FALSE is 0. Then SUM correctly tallies what you asked for.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 1 hour ago









                Rick JamesRick James

                41.5k22258




                41.5k22258






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Database Administrators Stack Exchange!


                    • 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%2fdba.stackexchange.com%2fquestions%2f227576%2fgetting-aggregate-sub-values-in-aggregate-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

                    الفوسفات في المغرب

                    Four equal circles intersect: What is the area of the small shaded portion and its height

                    جامعة ليفربول