Update column with the sum of another one











up vote
1
down vote

favorite












I used PgAdmin4 to develop a PostGIS database.
I tried to update a column (called "surface_net") in a table called "parcelles" with the sum of another column (called "surface_cultures") in another table called "zones_cultures". The table "zones_cultures" has a foreign key with "parcelles" ("zones_cultures.id_parcelles" = "parcelles.id_egrid").



To sum up, the column "surface_net" is the sum of "zones_cultures" group by the "id_egrid".



So I did this SQL query to update the column "surface_net":



UPDATE public.parcelles
SET surface_net=
(SELECT sum(zones_cultures.surface_cultures)
FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
group by parcelles.id_egrid);


But it doesn't work. Here is the result:



ERROR:  more than one row returned by a subquery used as an expression
SQL state: 21000


Could someone help me with my SQL query to be able to update the column "surface_net"?
Thanks in advance










share|improve this question




















  • 1




    Skip the GROUP BY
    – Joakim Danielson
    Nov 20 at 13:35










  • Show us db schema, sample data, current and expected output. Please read How-to-Ask And here is a great place to START to learn how improve your question quality and get better answers. How to create a Minimal, Complete, and Verifiable example
    – Juan Carlos Oropeza
    Nov 20 at 13:38















up vote
1
down vote

favorite












I used PgAdmin4 to develop a PostGIS database.
I tried to update a column (called "surface_net") in a table called "parcelles" with the sum of another column (called "surface_cultures") in another table called "zones_cultures". The table "zones_cultures" has a foreign key with "parcelles" ("zones_cultures.id_parcelles" = "parcelles.id_egrid").



To sum up, the column "surface_net" is the sum of "zones_cultures" group by the "id_egrid".



So I did this SQL query to update the column "surface_net":



UPDATE public.parcelles
SET surface_net=
(SELECT sum(zones_cultures.surface_cultures)
FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
group by parcelles.id_egrid);


But it doesn't work. Here is the result:



ERROR:  more than one row returned by a subquery used as an expression
SQL state: 21000


Could someone help me with my SQL query to be able to update the column "surface_net"?
Thanks in advance










share|improve this question




















  • 1




    Skip the GROUP BY
    – Joakim Danielson
    Nov 20 at 13:35










  • Show us db schema, sample data, current and expected output. Please read How-to-Ask And here is a great place to START to learn how improve your question quality and get better answers. How to create a Minimal, Complete, and Verifiable example
    – Juan Carlos Oropeza
    Nov 20 at 13:38













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I used PgAdmin4 to develop a PostGIS database.
I tried to update a column (called "surface_net") in a table called "parcelles" with the sum of another column (called "surface_cultures") in another table called "zones_cultures". The table "zones_cultures" has a foreign key with "parcelles" ("zones_cultures.id_parcelles" = "parcelles.id_egrid").



To sum up, the column "surface_net" is the sum of "zones_cultures" group by the "id_egrid".



So I did this SQL query to update the column "surface_net":



UPDATE public.parcelles
SET surface_net=
(SELECT sum(zones_cultures.surface_cultures)
FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
group by parcelles.id_egrid);


But it doesn't work. Here is the result:



ERROR:  more than one row returned by a subquery used as an expression
SQL state: 21000


Could someone help me with my SQL query to be able to update the column "surface_net"?
Thanks in advance










share|improve this question















I used PgAdmin4 to develop a PostGIS database.
I tried to update a column (called "surface_net") in a table called "parcelles" with the sum of another column (called "surface_cultures") in another table called "zones_cultures". The table "zones_cultures" has a foreign key with "parcelles" ("zones_cultures.id_parcelles" = "parcelles.id_egrid").



To sum up, the column "surface_net" is the sum of "zones_cultures" group by the "id_egrid".



So I did this SQL query to update the column "surface_net":



UPDATE public.parcelles
SET surface_net=
(SELECT sum(zones_cultures.surface_cultures)
FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
group by parcelles.id_egrid);


But it doesn't work. Here is the result:



ERROR:  more than one row returned by a subquery used as an expression
SQL state: 21000


Could someone help me with my SQL query to be able to update the column "surface_net"?
Thanks in advance







sql postgresql sql-update qsqlquery postgresql-11






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 13:35









a_horse_with_no_name

290k46439535




290k46439535










asked Nov 20 at 13:31









Sophie Compagnon

82




82








  • 1




    Skip the GROUP BY
    – Joakim Danielson
    Nov 20 at 13:35










  • Show us db schema, sample data, current and expected output. Please read How-to-Ask And here is a great place to START to learn how improve your question quality and get better answers. How to create a Minimal, Complete, and Verifiable example
    – Juan Carlos Oropeza
    Nov 20 at 13:38














  • 1




    Skip the GROUP BY
    – Joakim Danielson
    Nov 20 at 13:35










  • Show us db schema, sample data, current and expected output. Please read How-to-Ask And here is a great place to START to learn how improve your question quality and get better answers. How to create a Minimal, Complete, and Verifiable example
    – Juan Carlos Oropeza
    Nov 20 at 13:38








1




1




Skip the GROUP BY
– Joakim Danielson
Nov 20 at 13:35




Skip the GROUP BY
– Joakim Danielson
Nov 20 at 13:35












Show us db schema, sample data, current and expected output. Please read How-to-Ask And here is a great place to START to learn how improve your question quality and get better answers. How to create a Minimal, Complete, and Verifiable example
– Juan Carlos Oropeza
Nov 20 at 13:38




Show us db schema, sample data, current and expected output. Please read How-to-Ask And here is a great place to START to learn how improve your question quality and get better answers. How to create a Minimal, Complete, and Verifiable example
– Juan Carlos Oropeza
Nov 20 at 13:38












3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










You need a WHERE clause in the subquery that makes it return 1 relevant result for 1 row of the table you're updating. I believe this should work:



UPDATE public.parcelles p
SET surface_net=
(SELECT sum(zones_cultures.surface_cultures)
FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
where parcelles.id_egrid = p.id_egrid
group by parcelles.id_egrid);





share|improve this answer





















  • You dont need GROUP BY if already filter by egrid
    – Juan Carlos Oropeza
    Nov 20 at 13:41










  • Thanks a lot it worked!
    – Sophie Compagnon
    Nov 21 at 9:23










  • I'm glad it did. Don't forget to upvote and accept if this worked for you.
    – wvdz
    Nov 21 at 11:46










  • Just a last question: do you know how I could create an "auto update" with this code? I want that every time I add new values in the "zones_cultures", the "parcelles" table is update automatically with the update you gave me.
    – Sophie Compagnon
    Nov 21 at 16:11










  • @SophieCompagnon In general, you can do this in two ways: have parcelles be a view instead of a table, or by using a trigger. Using triggers should usually be avoided because they are a maintenance nightmare. So the best way to handle this is to create a view. If you need more help it would probably be best to create a new question for this.
    – wvdz
    Nov 24 at 0:11




















up vote
0
down vote













when you applied group by it returned multiple values as a result it thrown error, just remove group by it will work



UPDATE public.parcelles
SET surface_net=
(SELECT sum(zones_cultures.surface_cultures)
FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
);


Or if you want to update per id then use below no need group by



UPDATE public.parcelles p
SET p.surface_net=
(SELECT sum(zones_cultures.surface_cultures)
FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
where parcelles.id_egrid = p.id_egrid
);





share|improve this answer




























    up vote
    0
    down vote













    You can try below



    UPDATE public.parcelles p
    SET surface_net=sum(zones_cultures.surface_cultures)
    inner join zones_cultures where p.id_egrid = zones_cultures.id_parcelles





    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%2f53394141%2fupdate-column-with-the-sum-of-another-one%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








      up vote
      0
      down vote



      accepted










      You need a WHERE clause in the subquery that makes it return 1 relevant result for 1 row of the table you're updating. I believe this should work:



      UPDATE public.parcelles p
      SET surface_net=
      (SELECT sum(zones_cultures.surface_cultures)
      FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
      where parcelles.id_egrid = p.id_egrid
      group by parcelles.id_egrid);





      share|improve this answer





















      • You dont need GROUP BY if already filter by egrid
        – Juan Carlos Oropeza
        Nov 20 at 13:41










      • Thanks a lot it worked!
        – Sophie Compagnon
        Nov 21 at 9:23










      • I'm glad it did. Don't forget to upvote and accept if this worked for you.
        – wvdz
        Nov 21 at 11:46










      • Just a last question: do you know how I could create an "auto update" with this code? I want that every time I add new values in the "zones_cultures", the "parcelles" table is update automatically with the update you gave me.
        – Sophie Compagnon
        Nov 21 at 16:11










      • @SophieCompagnon In general, you can do this in two ways: have parcelles be a view instead of a table, or by using a trigger. Using triggers should usually be avoided because they are a maintenance nightmare. So the best way to handle this is to create a view. If you need more help it would probably be best to create a new question for this.
        – wvdz
        Nov 24 at 0:11

















      up vote
      0
      down vote



      accepted










      You need a WHERE clause in the subquery that makes it return 1 relevant result for 1 row of the table you're updating. I believe this should work:



      UPDATE public.parcelles p
      SET surface_net=
      (SELECT sum(zones_cultures.surface_cultures)
      FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
      where parcelles.id_egrid = p.id_egrid
      group by parcelles.id_egrid);





      share|improve this answer





















      • You dont need GROUP BY if already filter by egrid
        – Juan Carlos Oropeza
        Nov 20 at 13:41










      • Thanks a lot it worked!
        – Sophie Compagnon
        Nov 21 at 9:23










      • I'm glad it did. Don't forget to upvote and accept if this worked for you.
        – wvdz
        Nov 21 at 11:46










      • Just a last question: do you know how I could create an "auto update" with this code? I want that every time I add new values in the "zones_cultures", the "parcelles" table is update automatically with the update you gave me.
        – Sophie Compagnon
        Nov 21 at 16:11










      • @SophieCompagnon In general, you can do this in two ways: have parcelles be a view instead of a table, or by using a trigger. Using triggers should usually be avoided because they are a maintenance nightmare. So the best way to handle this is to create a view. If you need more help it would probably be best to create a new question for this.
        – wvdz
        Nov 24 at 0:11















      up vote
      0
      down vote



      accepted







      up vote
      0
      down vote



      accepted






      You need a WHERE clause in the subquery that makes it return 1 relevant result for 1 row of the table you're updating. I believe this should work:



      UPDATE public.parcelles p
      SET surface_net=
      (SELECT sum(zones_cultures.surface_cultures)
      FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
      where parcelles.id_egrid = p.id_egrid
      group by parcelles.id_egrid);





      share|improve this answer












      You need a WHERE clause in the subquery that makes it return 1 relevant result for 1 row of the table you're updating. I believe this should work:



      UPDATE public.parcelles p
      SET surface_net=
      (SELECT sum(zones_cultures.surface_cultures)
      FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
      where parcelles.id_egrid = p.id_egrid
      group by parcelles.id_egrid);






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 20 at 13:38









      wvdz

      12.5k22962




      12.5k22962












      • You dont need GROUP BY if already filter by egrid
        – Juan Carlos Oropeza
        Nov 20 at 13:41










      • Thanks a lot it worked!
        – Sophie Compagnon
        Nov 21 at 9:23










      • I'm glad it did. Don't forget to upvote and accept if this worked for you.
        – wvdz
        Nov 21 at 11:46










      • Just a last question: do you know how I could create an "auto update" with this code? I want that every time I add new values in the "zones_cultures", the "parcelles" table is update automatically with the update you gave me.
        – Sophie Compagnon
        Nov 21 at 16:11










      • @SophieCompagnon In general, you can do this in two ways: have parcelles be a view instead of a table, or by using a trigger. Using triggers should usually be avoided because they are a maintenance nightmare. So the best way to handle this is to create a view. If you need more help it would probably be best to create a new question for this.
        – wvdz
        Nov 24 at 0:11




















      • You dont need GROUP BY if already filter by egrid
        – Juan Carlos Oropeza
        Nov 20 at 13:41










      • Thanks a lot it worked!
        – Sophie Compagnon
        Nov 21 at 9:23










      • I'm glad it did. Don't forget to upvote and accept if this worked for you.
        – wvdz
        Nov 21 at 11:46










      • Just a last question: do you know how I could create an "auto update" with this code? I want that every time I add new values in the "zones_cultures", the "parcelles" table is update automatically with the update you gave me.
        – Sophie Compagnon
        Nov 21 at 16:11










      • @SophieCompagnon In general, you can do this in two ways: have parcelles be a view instead of a table, or by using a trigger. Using triggers should usually be avoided because they are a maintenance nightmare. So the best way to handle this is to create a view. If you need more help it would probably be best to create a new question for this.
        – wvdz
        Nov 24 at 0:11


















      You dont need GROUP BY if already filter by egrid
      – Juan Carlos Oropeza
      Nov 20 at 13:41




      You dont need GROUP BY if already filter by egrid
      – Juan Carlos Oropeza
      Nov 20 at 13:41












      Thanks a lot it worked!
      – Sophie Compagnon
      Nov 21 at 9:23




      Thanks a lot it worked!
      – Sophie Compagnon
      Nov 21 at 9:23












      I'm glad it did. Don't forget to upvote and accept if this worked for you.
      – wvdz
      Nov 21 at 11:46




      I'm glad it did. Don't forget to upvote and accept if this worked for you.
      – wvdz
      Nov 21 at 11:46












      Just a last question: do you know how I could create an "auto update" with this code? I want that every time I add new values in the "zones_cultures", the "parcelles" table is update automatically with the update you gave me.
      – Sophie Compagnon
      Nov 21 at 16:11




      Just a last question: do you know how I could create an "auto update" with this code? I want that every time I add new values in the "zones_cultures", the "parcelles" table is update automatically with the update you gave me.
      – Sophie Compagnon
      Nov 21 at 16:11












      @SophieCompagnon In general, you can do this in two ways: have parcelles be a view instead of a table, or by using a trigger. Using triggers should usually be avoided because they are a maintenance nightmare. So the best way to handle this is to create a view. If you need more help it would probably be best to create a new question for this.
      – wvdz
      Nov 24 at 0:11






      @SophieCompagnon In general, you can do this in two ways: have parcelles be a view instead of a table, or by using a trigger. Using triggers should usually be avoided because they are a maintenance nightmare. So the best way to handle this is to create a view. If you need more help it would probably be best to create a new question for this.
      – wvdz
      Nov 24 at 0:11














      up vote
      0
      down vote













      when you applied group by it returned multiple values as a result it thrown error, just remove group by it will work



      UPDATE public.parcelles
      SET surface_net=
      (SELECT sum(zones_cultures.surface_cultures)
      FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
      );


      Or if you want to update per id then use below no need group by



      UPDATE public.parcelles p
      SET p.surface_net=
      (SELECT sum(zones_cultures.surface_cultures)
      FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
      where parcelles.id_egrid = p.id_egrid
      );





      share|improve this answer

























        up vote
        0
        down vote













        when you applied group by it returned multiple values as a result it thrown error, just remove group by it will work



        UPDATE public.parcelles
        SET surface_net=
        (SELECT sum(zones_cultures.surface_cultures)
        FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
        );


        Or if you want to update per id then use below no need group by



        UPDATE public.parcelles p
        SET p.surface_net=
        (SELECT sum(zones_cultures.surface_cultures)
        FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
        where parcelles.id_egrid = p.id_egrid
        );





        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          when you applied group by it returned multiple values as a result it thrown error, just remove group by it will work



          UPDATE public.parcelles
          SET surface_net=
          (SELECT sum(zones_cultures.surface_cultures)
          FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
          );


          Or if you want to update per id then use below no need group by



          UPDATE public.parcelles p
          SET p.surface_net=
          (SELECT sum(zones_cultures.surface_cultures)
          FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
          where parcelles.id_egrid = p.id_egrid
          );





          share|improve this answer












          when you applied group by it returned multiple values as a result it thrown error, just remove group by it will work



          UPDATE public.parcelles
          SET surface_net=
          (SELECT sum(zones_cultures.surface_cultures)
          FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
          );


          Or if you want to update per id then use below no need group by



          UPDATE public.parcelles p
          SET p.surface_net=
          (SELECT sum(zones_cultures.surface_cultures)
          FROM parcelles inner join zones_cultures on parcelles.id_egrid = zones_cultures.id_parcelles
          where parcelles.id_egrid = p.id_egrid
          );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 13:38









          Zaynul Abadin Tuhin

          11.1k2831




          11.1k2831






















              up vote
              0
              down vote













              You can try below



              UPDATE public.parcelles p
              SET surface_net=sum(zones_cultures.surface_cultures)
              inner join zones_cultures where p.id_egrid = zones_cultures.id_parcelles





              share|improve this answer

























                up vote
                0
                down vote













                You can try below



                UPDATE public.parcelles p
                SET surface_net=sum(zones_cultures.surface_cultures)
                inner join zones_cultures where p.id_egrid = zones_cultures.id_parcelles





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You can try below



                  UPDATE public.parcelles p
                  SET surface_net=sum(zones_cultures.surface_cultures)
                  inner join zones_cultures where p.id_egrid = zones_cultures.id_parcelles





                  share|improve this answer












                  You can try below



                  UPDATE public.parcelles p
                  SET surface_net=sum(zones_cultures.surface_cultures)
                  inner join zones_cultures where p.id_egrid = zones_cultures.id_parcelles






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 at 13:39









                  fa06

                  10.2k1917




                  10.2k1917






























                      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%2f53394141%2fupdate-column-with-the-sum-of-another-one%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

                      Feedback on college project

                      Futebolista

                      Albești (Vaslui)