Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. how to update a sum value from join tables?

how to update a sum value from join tables?

Scheduled Pinned Locked Moved Database
helpdatabasetutorialquestionannouncement
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eunice VB junior
    wrote on last edited by
    #1

    Hi, I wrote this update query. But it doesn't work. The error message : An aggregate may not appear in the set list of an UPDATE statement. Here is my query. update udtStudent set score=sum(b.studentscore) from udtStudent a inner join udtQuestionHistory b on a.testid=b.testid and a.empno=b.empno where a.score is null group by b.empno,b.testid i wish to update the score = sum of studentscore from udtQuestionHistory the problem is at sum().Can anyone advice me how should i overcome it? Thanks in advance. thanks.

    J P 2 Replies Last reply
    0
    • E Eunice VB junior

      Hi, I wrote this update query. But it doesn't work. The error message : An aggregate may not appear in the set list of an UPDATE statement. Here is my query. update udtStudent set score=sum(b.studentscore) from udtStudent a inner join udtQuestionHistory b on a.testid=b.testid and a.empno=b.empno where a.score is null group by b.empno,b.testid i wish to update the score = sum of studentscore from udtQuestionHistory the problem is at sum().Can anyone advice me how should i overcome it? Thanks in advance. thanks.

      J Offline
      J Offline
      Joe 2
      wrote on last edited by
      #2

      Eunice (VB junior) wrote:

      set score=sum(b.studentscore) from udtStudent a inner join udtQuestionHistory b on a.testid=b.testid and a.empno=b.empno where a.score is null group by b.empno,b.testid

      Try to bring the sum in a select clause and assign it to an integer variable. For instance, Declare @intSum INT select @intSum = sum(score) from student a inner join history b on a.testid = b.testid Now, @intSum will hold the sum of studentscore from history table. Later, proceed with your update statement like update student set score = @intSum where ##your condition## Hope that thought might help you.

      E 1 Reply Last reply
      0
      • E Eunice VB junior

        Hi, I wrote this update query. But it doesn't work. The error message : An aggregate may not appear in the set list of an UPDATE statement. Here is my query. update udtStudent set score=sum(b.studentscore) from udtStudent a inner join udtQuestionHistory b on a.testid=b.testid and a.empno=b.empno where a.score is null group by b.empno,b.testid i wish to update the score = sum of studentscore from udtQuestionHistory the problem is at sum().Can anyone advice me how should i overcome it? Thanks in advance. thanks.

        P Offline
        P Offline
        pmarfleet
        wrote on last edited by
        #3

        You've already asked this question once. Please don't double-post.

        Paul Marfleet

        E 1 Reply Last reply
        0
        • J Joe 2

          Eunice (VB junior) wrote:

          set score=sum(b.studentscore) from udtStudent a inner join udtQuestionHistory b on a.testid=b.testid and a.empno=b.empno where a.score is null group by b.empno,b.testid

          Try to bring the sum in a select clause and assign it to an integer variable. For instance, Declare @intSum INT select @intSum = sum(score) from student a inner join history b on a.testid = b.testid Now, @intSum will hold the sum of studentscore from history table. Later, proceed with your update statement like update student set score = @intSum where ##your condition## Hope that thought might help you.

          E Offline
          E Offline
          Eunice VB junior
          wrote on last edited by
          #4

          Hi CS, thanks for the guidance. I got what you meant. Here is what i did according to your advice and it works. Declare @intSum INT declare @testID int declare @empno int select @intSum = sum(studentscore),@testid=testid,@empno=empno from udtquestionhistory group by empno,testid,questype,testtype,partnumber,process update udtstudent set score=@intSum where testid=@testid and empno=@empno Thanks for the help.

          1 Reply Last reply
          0
          • P pmarfleet

            You've already asked this question once. Please don't double-post.

            Paul Marfleet

            E Offline
            E Offline
            Eunice VB junior
            wrote on last edited by
            #5

            Hi pmarfleet, Actually there are different questions. The 2nd question is about subselect. But i'm using the same table as my query testing. Anyway, thanks for the input.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups