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 7713997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:00:49+00:00 2026-06-01T02:00:49+00:00

I am using java on sql server and I have a DB problem of

  • 0

I am using java on sql server and I have a DB problem of duplicate customers
(many customers are duplicated in the following way).
My tables are :

CUSTOMER(person_id,customer_id)
ORDER (order_id,person_id )

and a duplicated customer looks like this :

CUSTOMER(333,040535743)
CUSTOMER(334,040535743)
CUSTOMER(335,040535743)

ORDER (1,333 )
ORDER (2,333 )

ORDER (4,334 )
ORDER (5,334 )
ORDER (6,334 )

ORDER (7,335 )
ORDER (8,335 )
ORDER (9,335 )


CUSTOMER(336,009530650)
CUSTOMER(337,009530650)

ORDER (10,336 )
ORDER (11,336 )
ORDER (12,336 )

ORDER (13,337 )
ORDER (14,337 )
ORDER (15,337 )

333,334,335 are three rows for the same customer because they have the same customer_id, I want to keep only the last customer ( 335), and make all the orders of 334 and 333 refer to 333.

my final data should be :

CUSTOMER(335,040535743)
ORDER (1,335)
ORDER (2,335)

ORDER (4,335)
ORDER (5,335)
ORDER (6,335)

ORDER (7,335 )
ORDER (8,335 )
ORDER (9,335 )



CUSTOMER(337,009530650)

ORDER (10,337)
ORDER (11,337)
ORDER (12,337)

ORDER (13,337 )
ORDER (14,337 )
ORDER (15,337 )

how do I do a query that does that for all my duplicated customers? (I have a query returning the list of the customer_id’s that have duplications)

  • 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-01T02:00:50+00:00Added an answer on June 1, 2026 at 2:00 am

    Update the Order table:

    UPDATE o
    SET o.person_id = cc.max_person_id
    FROM
        [Order] AS o
      JOIN
        Customer AS c
            ON c.person_id = o.person_id
      JOIN
        ( SELECT customer_id
               , MAX(person_id) AS max_person_id
          FROM Customer
          GROUP BY customer_id
        ) AS cc
            ON cc.customer_id = c.customer_id ;
    

    Then, update the Customer table:

    UPDATE c
    SET c.person_id = cc.max_person_id
    FROM
        Customer AS c
      JOIN
        ( SELECT customer_id
               , MAX(person_id) AS max_person_id
          FROM Customer
          GROUP BY customer_id
        ) AS cc
            ON cc.customer_id = c.customer_id ;
    

    After that, it would be good to have Customer(person_id) defined as PRIMARY KEY or with a UNIQUE constraint.

    And a FOREIGN KEY constraint from Order(person_id) to Customer(person_id)

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

Sidebar

Related Questions

I created and launch the SQLJ Java procedure (using SQL Developer). I have some
I have a view defined in SQL server 2008 that joins 4 tables together.
I have both java and .net applications running on an app server using Microsoft
I have a problem with Hibernate (3.6.0.-Final) in my Java EE application using MySQL
I'm using eclipse, trying to connect to a sql server db. I have the
I have a problem where I get a deadlock on a MS SQL Server.
I'm working on a reporting system using Java 6 and SQL Server 2008. For
The weird behavior is that a java.sql.Timestamp that I create using the System.currentTimeMillis() method,
I'm using Hibernate with JPA and MySQL. I got accessing denied. java.sql.SQLException: Access denied
Using Java sockets, I made a simple server. This works because it sends data

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.