How to subtract the average value of a column from the same column?












0















I have a MySQL table of one column [20, 60, 40, 20] and the average value of this column is 35. I would like to subtract the average value (in this case 35) from the same column such that the resulting column should have [-15, 25, 5, -15].



CREATE TABLE TEST (LENGTH FLOAT);
INSERT INTO TEST (LENGTH) VALUES (20), (60), (40), (20);


The result of the MySQL should be [-15, 25, 5, -15]



Thanks!










share|improve this question

























  • "I would like to subtract the average value (in this case 40)" Why is 40 the average value here? 35 is the average here do you mean something else here?

    – Raymond Nijland
    Jan 20 at 3:13













  • Sorry, my bad. The average value in this case is 35! I would like to subtract the average value of this column (i.e. (20+60+40+20)/4.) from the same column. Eg. LENGTH column has [20, 60, 40, 20] and it should turn in to [-15, 25, 5, -15]

    – vinu
    Jan 20 at 3:54
















0















I have a MySQL table of one column [20, 60, 40, 20] and the average value of this column is 35. I would like to subtract the average value (in this case 35) from the same column such that the resulting column should have [-15, 25, 5, -15].



CREATE TABLE TEST (LENGTH FLOAT);
INSERT INTO TEST (LENGTH) VALUES (20), (60), (40), (20);


The result of the MySQL should be [-15, 25, 5, -15]



Thanks!










share|improve this question

























  • "I would like to subtract the average value (in this case 40)" Why is 40 the average value here? 35 is the average here do you mean something else here?

    – Raymond Nijland
    Jan 20 at 3:13













  • Sorry, my bad. The average value in this case is 35! I would like to subtract the average value of this column (i.e. (20+60+40+20)/4.) from the same column. Eg. LENGTH column has [20, 60, 40, 20] and it should turn in to [-15, 25, 5, -15]

    – vinu
    Jan 20 at 3:54














0












0








0








I have a MySQL table of one column [20, 60, 40, 20] and the average value of this column is 35. I would like to subtract the average value (in this case 35) from the same column such that the resulting column should have [-15, 25, 5, -15].



CREATE TABLE TEST (LENGTH FLOAT);
INSERT INTO TEST (LENGTH) VALUES (20), (60), (40), (20);


The result of the MySQL should be [-15, 25, 5, -15]



Thanks!










share|improve this question
















I have a MySQL table of one column [20, 60, 40, 20] and the average value of this column is 35. I would like to subtract the average value (in this case 35) from the same column such that the resulting column should have [-15, 25, 5, -15].



CREATE TABLE TEST (LENGTH FLOAT);
INSERT INTO TEST (LENGTH) VALUES (20), (60), (40), (20);


The result of the MySQL should be [-15, 25, 5, -15]



Thanks!







mysql sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 at 6:42









Fabien TheSolution

4,4871726




4,4871726










asked Jan 20 at 3:06









vinuvinu

185




185













  • "I would like to subtract the average value (in this case 40)" Why is 40 the average value here? 35 is the average here do you mean something else here?

    – Raymond Nijland
    Jan 20 at 3:13













  • Sorry, my bad. The average value in this case is 35! I would like to subtract the average value of this column (i.e. (20+60+40+20)/4.) from the same column. Eg. LENGTH column has [20, 60, 40, 20] and it should turn in to [-15, 25, 5, -15]

    – vinu
    Jan 20 at 3:54



















  • "I would like to subtract the average value (in this case 40)" Why is 40 the average value here? 35 is the average here do you mean something else here?

    – Raymond Nijland
    Jan 20 at 3:13













  • Sorry, my bad. The average value in this case is 35! I would like to subtract the average value of this column (i.e. (20+60+40+20)/4.) from the same column. Eg. LENGTH column has [20, 60, 40, 20] and it should turn in to [-15, 25, 5, -15]

    – vinu
    Jan 20 at 3:54

















"I would like to subtract the average value (in this case 40)" Why is 40 the average value here? 35 is the average here do you mean something else here?

– Raymond Nijland
Jan 20 at 3:13







"I would like to subtract the average value (in this case 40)" Why is 40 the average value here? 35 is the average here do you mean something else here?

– Raymond Nijland
Jan 20 at 3:13















Sorry, my bad. The average value in this case is 35! I would like to subtract the average value of this column (i.e. (20+60+40+20)/4.) from the same column. Eg. LENGTH column has [20, 60, 40, 20] and it should turn in to [-15, 25, 5, -15]

– vinu
Jan 20 at 3:54





Sorry, my bad. The average value in this case is 35! I would like to subtract the average value of this column (i.e. (20+60+40+20)/4.) from the same column. Eg. LENGTH column has [20, 60, 40, 20] and it should turn in to [-15, 25, 5, -15]

– vinu
Jan 20 at 3:54












3 Answers
3






active

oldest

votes


















2














Using a subquery may help:



SELECT a.length - b.length_avg
FROM
test a, (
SELECT AVG(length) AS "LENGTH_AVG"
FROM test
) b
;


You may need to fix the syntax ... I didn't test it ... but the idea is to execute a query like that ... If you want more info, google: mysql subqueries






share|improve this answer
























  • That worked :) Thanks Carlitos !

    – vinu
    Jan 20 at 4:38



















1














You can do shorter with something like this :



SELECT LENGTH-(SELECT AVG(LENGTH) FROM TEST) FROM TEST





share|improve this answer































    1














    In MySQL 8+, you would do:



    select (t.length - avg(t.length) over ())
    from test t;


    In earlier versions, I would phrase this as:



    select t.length - tt.avg_length
    from test t cross join
    (select avg(length) as avg_length from test) tt;





    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%2f54273251%2fhow-to-subtract-the-average-value-of-a-column-from-the-same-column%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









      2














      Using a subquery may help:



      SELECT a.length - b.length_avg
      FROM
      test a, (
      SELECT AVG(length) AS "LENGTH_AVG"
      FROM test
      ) b
      ;


      You may need to fix the syntax ... I didn't test it ... but the idea is to execute a query like that ... If you want more info, google: mysql subqueries






      share|improve this answer
























      • That worked :) Thanks Carlitos !

        – vinu
        Jan 20 at 4:38
















      2














      Using a subquery may help:



      SELECT a.length - b.length_avg
      FROM
      test a, (
      SELECT AVG(length) AS "LENGTH_AVG"
      FROM test
      ) b
      ;


      You may need to fix the syntax ... I didn't test it ... but the idea is to execute a query like that ... If you want more info, google: mysql subqueries






      share|improve this answer
























      • That worked :) Thanks Carlitos !

        – vinu
        Jan 20 at 4:38














      2












      2








      2







      Using a subquery may help:



      SELECT a.length - b.length_avg
      FROM
      test a, (
      SELECT AVG(length) AS "LENGTH_AVG"
      FROM test
      ) b
      ;


      You may need to fix the syntax ... I didn't test it ... but the idea is to execute a query like that ... If you want more info, google: mysql subqueries






      share|improve this answer













      Using a subquery may help:



      SELECT a.length - b.length_avg
      FROM
      test a, (
      SELECT AVG(length) AS "LENGTH_AVG"
      FROM test
      ) b
      ;


      You may need to fix the syntax ... I didn't test it ... but the idea is to execute a query like that ... If you want more info, google: mysql subqueries







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jan 20 at 4:14









      Carlitos WayCarlitos Way

      2,1191322




      2,1191322













      • That worked :) Thanks Carlitos !

        – vinu
        Jan 20 at 4:38



















      • That worked :) Thanks Carlitos !

        – vinu
        Jan 20 at 4:38

















      That worked :) Thanks Carlitos !

      – vinu
      Jan 20 at 4:38





      That worked :) Thanks Carlitos !

      – vinu
      Jan 20 at 4:38













      1














      You can do shorter with something like this :



      SELECT LENGTH-(SELECT AVG(LENGTH) FROM TEST) FROM TEST





      share|improve this answer




























        1














        You can do shorter with something like this :



        SELECT LENGTH-(SELECT AVG(LENGTH) FROM TEST) FROM TEST





        share|improve this answer


























          1












          1








          1







          You can do shorter with something like this :



          SELECT LENGTH-(SELECT AVG(LENGTH) FROM TEST) FROM TEST





          share|improve this answer













          You can do shorter with something like this :



          SELECT LENGTH-(SELECT AVG(LENGTH) FROM TEST) FROM TEST






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 20 at 6:17









          Fabien TheSolutionFabien TheSolution

          4,4871726




          4,4871726























              1














              In MySQL 8+, you would do:



              select (t.length - avg(t.length) over ())
              from test t;


              In earlier versions, I would phrase this as:



              select t.length - tt.avg_length
              from test t cross join
              (select avg(length) as avg_length from test) tt;





              share|improve this answer




























                1














                In MySQL 8+, you would do:



                select (t.length - avg(t.length) over ())
                from test t;


                In earlier versions, I would phrase this as:



                select t.length - tt.avg_length
                from test t cross join
                (select avg(length) as avg_length from test) tt;





                share|improve this answer


























                  1












                  1








                  1







                  In MySQL 8+, you would do:



                  select (t.length - avg(t.length) over ())
                  from test t;


                  In earlier versions, I would phrase this as:



                  select t.length - tt.avg_length
                  from test t cross join
                  (select avg(length) as avg_length from test) tt;





                  share|improve this answer













                  In MySQL 8+, you would do:



                  select (t.length - avg(t.length) over ())
                  from test t;


                  In earlier versions, I would phrase this as:



                  select t.length - tt.avg_length
                  from test t cross join
                  (select avg(length) as avg_length from test) tt;






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 20 at 12:22









                  Gordon LinoffGordon Linoff

                  772k35306406




                  772k35306406






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54273251%2fhow-to-subtract-the-average-value-of-a-column-from-the-same-column%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

                      Callistus III

                      Ostreoida

                      Plistias Cous