MYSQL: Increase value from row to row with GROUP BY










0














I would like to add up meter values from month to month. My data looks like this:



| Date | Energy_produced |
--------------------------------
| 2011-01-01 | 12 |
| 2017-01-02 | 23 |
| 2017-01-03 | 25 |
...
| 2011-02-01 | 11 |
| 2017-02-02 | 12 |
| 2017-02-03 | 17 |


The GROUP BY would result in the SUM of the values "Energy_produced" for each month but I would like the sum to be added to get:



| Month | Energy_produced_added_up |
--------------------------------------
| 2011-01 | 60 |
| 2017-02 | 100 |


I read a lot of articles and understood that I could use a variable:



set @result := 0;
SELECT
DATE_FORMAT(t.Datum_Maand, '%m') AS measurement_month,
DATE_FORMAT(t.Datum_Maand, '%Y') AS measurement_year,
(@result := @result + SUM(t.Geg_Maand)) AS SUMME
FROM tgeg_maand AS t
GROUP BY measurement_year, measurement_month
ORDER BY measurement_year, measurement_month


With GROUP BY, the value is not added up. The statement above displays the same result as:



SELECT 
DATE_FORMAT(t.Datum_Maand, '%m') AS measurement_month,
DATE_FORMAT(t.Datum_Maand, '%Y') AS measurement_year,
SUM(t.Geg_Maand) AS SUMME
FROM tgeg_maand AS t
GROUP BY measurement_year, measurement_month
ORDER BY measurement_year, measurement_month


What am I doing wrong?



Thanks










share|improve this question




























    0














    I would like to add up meter values from month to month. My data looks like this:



    | Date | Energy_produced |
    --------------------------------
    | 2011-01-01 | 12 |
    | 2017-01-02 | 23 |
    | 2017-01-03 | 25 |
    ...
    | 2011-02-01 | 11 |
    | 2017-02-02 | 12 |
    | 2017-02-03 | 17 |


    The GROUP BY would result in the SUM of the values "Energy_produced" for each month but I would like the sum to be added to get:



    | Month | Energy_produced_added_up |
    --------------------------------------
    | 2011-01 | 60 |
    | 2017-02 | 100 |


    I read a lot of articles and understood that I could use a variable:



    set @result := 0;
    SELECT
    DATE_FORMAT(t.Datum_Maand, '%m') AS measurement_month,
    DATE_FORMAT(t.Datum_Maand, '%Y') AS measurement_year,
    (@result := @result + SUM(t.Geg_Maand)) AS SUMME
    FROM tgeg_maand AS t
    GROUP BY measurement_year, measurement_month
    ORDER BY measurement_year, measurement_month


    With GROUP BY, the value is not added up. The statement above displays the same result as:



    SELECT 
    DATE_FORMAT(t.Datum_Maand, '%m') AS measurement_month,
    DATE_FORMAT(t.Datum_Maand, '%Y') AS measurement_year,
    SUM(t.Geg_Maand) AS SUMME
    FROM tgeg_maand AS t
    GROUP BY measurement_year, measurement_month
    ORDER BY measurement_year, measurement_month


    What am I doing wrong?



    Thanks










    share|improve this question


























      0












      0








      0







      I would like to add up meter values from month to month. My data looks like this:



      | Date | Energy_produced |
      --------------------------------
      | 2011-01-01 | 12 |
      | 2017-01-02 | 23 |
      | 2017-01-03 | 25 |
      ...
      | 2011-02-01 | 11 |
      | 2017-02-02 | 12 |
      | 2017-02-03 | 17 |


      The GROUP BY would result in the SUM of the values "Energy_produced" for each month but I would like the sum to be added to get:



      | Month | Energy_produced_added_up |
      --------------------------------------
      | 2011-01 | 60 |
      | 2017-02 | 100 |


      I read a lot of articles and understood that I could use a variable:



      set @result := 0;
      SELECT
      DATE_FORMAT(t.Datum_Maand, '%m') AS measurement_month,
      DATE_FORMAT(t.Datum_Maand, '%Y') AS measurement_year,
      (@result := @result + SUM(t.Geg_Maand)) AS SUMME
      FROM tgeg_maand AS t
      GROUP BY measurement_year, measurement_month
      ORDER BY measurement_year, measurement_month


      With GROUP BY, the value is not added up. The statement above displays the same result as:



      SELECT 
      DATE_FORMAT(t.Datum_Maand, '%m') AS measurement_month,
      DATE_FORMAT(t.Datum_Maand, '%Y') AS measurement_year,
      SUM(t.Geg_Maand) AS SUMME
      FROM tgeg_maand AS t
      GROUP BY measurement_year, measurement_month
      ORDER BY measurement_year, measurement_month


      What am I doing wrong?



      Thanks










      share|improve this question















      I would like to add up meter values from month to month. My data looks like this:



      | Date | Energy_produced |
      --------------------------------
      | 2011-01-01 | 12 |
      | 2017-01-02 | 23 |
      | 2017-01-03 | 25 |
      ...
      | 2011-02-01 | 11 |
      | 2017-02-02 | 12 |
      | 2017-02-03 | 17 |


      The GROUP BY would result in the SUM of the values "Energy_produced" for each month but I would like the sum to be added to get:



      | Month | Energy_produced_added_up |
      --------------------------------------
      | 2011-01 | 60 |
      | 2017-02 | 100 |


      I read a lot of articles and understood that I could use a variable:



      set @result := 0;
      SELECT
      DATE_FORMAT(t.Datum_Maand, '%m') AS measurement_month,
      DATE_FORMAT(t.Datum_Maand, '%Y') AS measurement_year,
      (@result := @result + SUM(t.Geg_Maand)) AS SUMME
      FROM tgeg_maand AS t
      GROUP BY measurement_year, measurement_month
      ORDER BY measurement_year, measurement_month


      With GROUP BY, the value is not added up. The statement above displays the same result as:



      SELECT 
      DATE_FORMAT(t.Datum_Maand, '%m') AS measurement_month,
      DATE_FORMAT(t.Datum_Maand, '%Y') AS measurement_year,
      SUM(t.Geg_Maand) AS SUMME
      FROM tgeg_maand AS t
      GROUP BY measurement_year, measurement_month
      ORDER BY measurement_year, measurement_month


      What am I doing wrong?



      Thanks







      mysql group-by






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 11:47









      Nick

      22.7k81535




      22.7k81535










      asked Nov 12 at 11:27









      AntonSack

      5301233




      5301233






















          2 Answers
          2






          active

          oldest

          votes


















          1














          You need to do the summing by month first and then sum the monthly values. For this you will need to make your original query into a subquery:



          set @result := 0;
          SELECT measurement_month, measurement_year,
          @result := @result + monthly_sum AS SUMME
          FROM (SELECT DATE_FORMAT(t.Datum_Maand, '%m') AS measurement_month,
          DATE_FORMAT(t.Datum_Maand, '%Y') AS measurement_year,
          SUM(t.Geg_Maand) AS monthly_sum
          FROM tgeg_maand AS t
          GROUP BY measurement_year, measurement_month) mm
          ORDER BY measurement_year, measurement_month





          share|improve this answer




















          • That's it! Thanks a lot!
            – AntonSack
            Nov 12 at 11:47










          • No worries - sorry about my original comment, I misinterpreted your question.
            – Nick
            Nov 12 at 11:49


















          0














          Use this query



          SELECT 
          DATE_FORMAT(t.Datum_Maand, '%m') AS measurement_month,
          DATE_FORMAT(t.Datum_Maand, '%Y') AS measurement_year,
          SUM(t.Geg_Maand) AS SUMME
          FROM tgeg_maand AS t
          GROUP BY measurement_year, measurement_month
          ORDER BY measurement_year, measurement_month





          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%2f53261206%2fmysql-increase-value-from-row-to-row-with-group-by%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You need to do the summing by month first and then sum the monthly values. For this you will need to make your original query into a subquery:



            set @result := 0;
            SELECT measurement_month, measurement_year,
            @result := @result + monthly_sum AS SUMME
            FROM (SELECT DATE_FORMAT(t.Datum_Maand, '%m') AS measurement_month,
            DATE_FORMAT(t.Datum_Maand, '%Y') AS measurement_year,
            SUM(t.Geg_Maand) AS monthly_sum
            FROM tgeg_maand AS t
            GROUP BY measurement_year, measurement_month) mm
            ORDER BY measurement_year, measurement_month





            share|improve this answer




















            • That's it! Thanks a lot!
              – AntonSack
              Nov 12 at 11:47










            • No worries - sorry about my original comment, I misinterpreted your question.
              – Nick
              Nov 12 at 11:49















            1














            You need to do the summing by month first and then sum the monthly values. For this you will need to make your original query into a subquery:



            set @result := 0;
            SELECT measurement_month, measurement_year,
            @result := @result + monthly_sum AS SUMME
            FROM (SELECT DATE_FORMAT(t.Datum_Maand, '%m') AS measurement_month,
            DATE_FORMAT(t.Datum_Maand, '%Y') AS measurement_year,
            SUM(t.Geg_Maand) AS monthly_sum
            FROM tgeg_maand AS t
            GROUP BY measurement_year, measurement_month) mm
            ORDER BY measurement_year, measurement_month





            share|improve this answer




















            • That's it! Thanks a lot!
              – AntonSack
              Nov 12 at 11:47










            • No worries - sorry about my original comment, I misinterpreted your question.
              – Nick
              Nov 12 at 11:49













            1












            1








            1






            You need to do the summing by month first and then sum the monthly values. For this you will need to make your original query into a subquery:



            set @result := 0;
            SELECT measurement_month, measurement_year,
            @result := @result + monthly_sum AS SUMME
            FROM (SELECT DATE_FORMAT(t.Datum_Maand, '%m') AS measurement_month,
            DATE_FORMAT(t.Datum_Maand, '%Y') AS measurement_year,
            SUM(t.Geg_Maand) AS monthly_sum
            FROM tgeg_maand AS t
            GROUP BY measurement_year, measurement_month) mm
            ORDER BY measurement_year, measurement_month





            share|improve this answer












            You need to do the summing by month first and then sum the monthly values. For this you will need to make your original query into a subquery:



            set @result := 0;
            SELECT measurement_month, measurement_year,
            @result := @result + monthly_sum AS SUMME
            FROM (SELECT DATE_FORMAT(t.Datum_Maand, '%m') AS measurement_month,
            DATE_FORMAT(t.Datum_Maand, '%Y') AS measurement_year,
            SUM(t.Geg_Maand) AS monthly_sum
            FROM tgeg_maand AS t
            GROUP BY measurement_year, measurement_month) mm
            ORDER BY measurement_year, measurement_month






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 12 at 11:45









            Nick

            22.7k81535




            22.7k81535











            • That's it! Thanks a lot!
              – AntonSack
              Nov 12 at 11:47










            • No worries - sorry about my original comment, I misinterpreted your question.
              – Nick
              Nov 12 at 11:49
















            • That's it! Thanks a lot!
              – AntonSack
              Nov 12 at 11:47










            • No worries - sorry about my original comment, I misinterpreted your question.
              – Nick
              Nov 12 at 11:49















            That's it! Thanks a lot!
            – AntonSack
            Nov 12 at 11:47




            That's it! Thanks a lot!
            – AntonSack
            Nov 12 at 11:47












            No worries - sorry about my original comment, I misinterpreted your question.
            – Nick
            Nov 12 at 11:49




            No worries - sorry about my original comment, I misinterpreted your question.
            – Nick
            Nov 12 at 11:49













            0














            Use this query



            SELECT 
            DATE_FORMAT(t.Datum_Maand, '%m') AS measurement_month,
            DATE_FORMAT(t.Datum_Maand, '%Y') AS measurement_year,
            SUM(t.Geg_Maand) AS SUMME
            FROM tgeg_maand AS t
            GROUP BY measurement_year, measurement_month
            ORDER BY measurement_year, measurement_month





            share|improve this answer

























              0














              Use this query



              SELECT 
              DATE_FORMAT(t.Datum_Maand, '%m') AS measurement_month,
              DATE_FORMAT(t.Datum_Maand, '%Y') AS measurement_year,
              SUM(t.Geg_Maand) AS SUMME
              FROM tgeg_maand AS t
              GROUP BY measurement_year, measurement_month
              ORDER BY measurement_year, measurement_month





              share|improve this answer























                0












                0








                0






                Use this query



                SELECT 
                DATE_FORMAT(t.Datum_Maand, '%m') AS measurement_month,
                DATE_FORMAT(t.Datum_Maand, '%Y') AS measurement_year,
                SUM(t.Geg_Maand) AS SUMME
                FROM tgeg_maand AS t
                GROUP BY measurement_year, measurement_month
                ORDER BY measurement_year, measurement_month





                share|improve this answer












                Use this query



                SELECT 
                DATE_FORMAT(t.Datum_Maand, '%m') AS measurement_month,
                DATE_FORMAT(t.Datum_Maand, '%Y') AS measurement_year,
                SUM(t.Geg_Maand) AS SUMME
                FROM tgeg_maand AS t
                GROUP BY measurement_year, measurement_month
                ORDER BY measurement_year, measurement_month






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 at 11:41









                Yash Fatnani

                1015




                1015



























                    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%2f53261206%2fmysql-increase-value-from-row-to-row-with-group-by%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

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    政党

                    天津地下鉄3号線