Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8654443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:47:21+00:00 2026-06-12T14:47:21+00:00

I have two tables: PERSON with columns person_id and total DATA with columns data_a

  • 0

I have two tables:

  • PERSON with columns person_id and total
  • DATA with columns data_a, data_b, data_c, and data_person_id

Each “person” can have zero or more entries in DATA – your standard one-to-many relationship. PERSON has a total column that is the sum of values in DATA. There are currently some discrepancies between total and the actual entries in DATA where DATA is correct but total is wrong.

This is the query I’m using to find the discrepancies:

SELECT
  person_id
FROM PERSON JOIN (
  SELECT
    data_person_id,
    SUM( data_a + data_b + data_c ) as data_total
  FROM
    DATA
  GROUP BY
    data_person_id
  ) x ON data_person_id = person_id
WHERE
  person_total != data_total

I plan on doing this through Hibernate as a query where the backend will be Postgres 9.x.

The incorrect query that I’m trying to understand/fix is:

UPDATE
  ONLY PERSON
SET
  total = data_info.calc_total
FROM (
  SELECT
    SUM( data_a + data_b + data_c ) as calc_total
  FROM
    DATA
  WHERE
    DATA.data_person_id = person_id
  GROUP BY
    DATA.data_person_id
) as data_info
WHERE
  PERSON.person_id IN (
    SELECT
      data_person_id
    FROM PERSON JOIN (
      SELECT
        data_person_id,
        SUM( data_a + data_b + data_c ) as data_total
      FROM
        DATA
      GROUP BY
        data_person_id
      ) x ON person_id = data_person_id
    WHERE
      total != data_total
  )

Right now, it won’t run because of WHERE DATA.data_person_id = person_id. But if I take that out, the wrong values get used.

The following seems to work but I’m confused as to why:

UPDATE
  ONLY PERSON
SET
  total = data_info.calc_total
FROM 
  PERSON P JOIN (
    SELECT
      data_person_id,
      SUM( data_a + data_b + data_c ) as calc_total
    FROM
      DATA
    WHERE
      DATA.data_person_id = person_id
    GROUP BY
      DATA.data_person_id
  ) as data_info ON P.person_id = data_person_id
WHERE
  PERSON.person_id IN (
    SELECT
      data_person_id
    FROM PERSON JOIN (
      SELECT
        data_person_id,
        SUM( data_a + data_b + data_c ) as data_total
      FROM
        DATA
      GROUP BY
        data_person_id
      ) x ON person_id = data_person_id
    WHERE
      total != data_total
  )

I believe my problem lies in my misunderstanding of the doc (I’m guessing the part about the self-join).

Also, any ways to improve this query is appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:47:23+00:00Added an answer on June 12, 2026 at 2:47 pm

    Seems your queries are way to complex. The task should be as simple as:

    UPDATE person p
    SET    total = d.calc_total
    FROM (
      SELECT data_person_id, sum(data_a + data_b + data_c) as calc_total
      FROM   data
      GROUP  BY 1
    ) d
    WHERE  p.person_id = d.data_person_id
    AND    p.total IS DISTINCT FROM d.calc_total;
    
    • First aggregate calc_total from the data table, grouped by data_person_id.

    • Then use this subquery in the FROM clause of the UPDATE.

    • I use IS DISTINCT FROM to make sure NULL values are covered, while only rows that would change are actually updated.
      If all involved columns are defined NOT NULL, you can use = instead.

    -> sqlfiddle demo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables one named Person , which contains columns ID and Name
I have a table with two columns: person_id person_id with whom 1st field id
I have these two tables people ============ id, name and answer_sheets ============ id, person_id,
I have two tables, lets say table1 and table2 with common columns, id and
I have two tables, House and Person . For any row in House, there
I have two tables: Issue and Return . Both tables have columns article_id ,
I have two tables (already created), say Person and Address with following schemas: create
I have two tables: Person {pID, pName, deptID} and Departments {deptID,deptName} An SQL statement
I have two tables called Person and Address . These tables I mapped one
I have two tables: persons - person_id - fullname students - student_id _ person_id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.