SQL Server - Merging one record to another and changing items referencing the first item to reference the...
up vote
0
down vote
favorite
I have a duplicate record in a SQL Server table with (obviously) different unique IDs.
CustomerId FirstName LastName|
1 John Doe |
2 John Doe |
There are child items that are already in the database (let's say an orders table), some relate to one of the items and some relate to the other.
OrderId CustomerId DateEntered|
100 1 2018/11/01 |
101 2 2018/11/09 |
They are both referring to the same customer.
I would like to delete the duplicate record. Is there a way to automatically update the CustomerIds in the Orders table that has a value of 2 to have a value of 1?
add a comment |
up vote
0
down vote
favorite
I have a duplicate record in a SQL Server table with (obviously) different unique IDs.
CustomerId FirstName LastName|
1 John Doe |
2 John Doe |
There are child items that are already in the database (let's say an orders table), some relate to one of the items and some relate to the other.
OrderId CustomerId DateEntered|
100 1 2018/11/01 |
101 2 2018/11/09 |
They are both referring to the same customer.
I would like to delete the duplicate record. Is there a way to automatically update the CustomerIds in the Orders table that has a value of 2 to have a value of 1?
Sample data and expected results as formatted text please not images.
– Sami
Nov 18 at 20:02
1
The short answer is NO... Nothing that would work automatically. You'd have to find all FK references to "CustomerID" (hopefully the database in question actually has good DRI) and generate commands to update their values.
– Jason A. Long
Nov 18 at 21:13
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a duplicate record in a SQL Server table with (obviously) different unique IDs.
CustomerId FirstName LastName|
1 John Doe |
2 John Doe |
There are child items that are already in the database (let's say an orders table), some relate to one of the items and some relate to the other.
OrderId CustomerId DateEntered|
100 1 2018/11/01 |
101 2 2018/11/09 |
They are both referring to the same customer.
I would like to delete the duplicate record. Is there a way to automatically update the CustomerIds in the Orders table that has a value of 2 to have a value of 1?
I have a duplicate record in a SQL Server table with (obviously) different unique IDs.
CustomerId FirstName LastName|
1 John Doe |
2 John Doe |
There are child items that are already in the database (let's say an orders table), some relate to one of the items and some relate to the other.
OrderId CustomerId DateEntered|
100 1 2018/11/01 |
101 2 2018/11/09 |
They are both referring to the same customer.
I would like to delete the duplicate record. Is there a way to automatically update the CustomerIds in the Orders table that has a value of 2 to have a value of 1?
edited Nov 18 at 20:25
asked Nov 18 at 19:51
Izzy
313
313
Sample data and expected results as formatted text please not images.
– Sami
Nov 18 at 20:02
1
The short answer is NO... Nothing that would work automatically. You'd have to find all FK references to "CustomerID" (hopefully the database in question actually has good DRI) and generate commands to update their values.
– Jason A. Long
Nov 18 at 21:13
add a comment |
Sample data and expected results as formatted text please not images.
– Sami
Nov 18 at 20:02
1
The short answer is NO... Nothing that would work automatically. You'd have to find all FK references to "CustomerID" (hopefully the database in question actually has good DRI) and generate commands to update their values.
– Jason A. Long
Nov 18 at 21:13
Sample data and expected results as formatted text please not images.
– Sami
Nov 18 at 20:02
Sample data and expected results as formatted text please not images.
– Sami
Nov 18 at 20:02
1
1
The short answer is NO... Nothing that would work automatically. You'd have to find all FK references to "CustomerID" (hopefully the database in question actually has good DRI) and generate commands to update their values.
– Jason A. Long
Nov 18 at 21:13
The short answer is NO... Nothing that would work automatically. You'd have to find all FK references to "CustomerID" (hopefully the database in question actually has good DRI) and generate commands to update their values.
– Jason A. Long
Nov 18 at 21:13
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You can use a window function to solve this
WITH CTE AS
(
SELECT T1.*,
MIN(T1.CustomerId) OVER(PARTITION BY T1.FirstName, T1.LastName ORDER BY T1.CustomerId) RN
FROM T1
)
UPDATE T2
SET T2.CustomerId = CTE.RN
FROM T2 INNER JOIN CTE ON T2.CustomerId = CTE.CustomerId;
Demo
But in the first place you need to fix this, the customer should have one(1) Unique id, not multiple IDs.
Thank you. What I wanted to know was if SQL Server has built in that the records in the other tables that relate to this record can automatically be updated to the record it was merged into without specifically updating the CustomerId in the record in T2.
– Izzy
Nov 19 at 22:07
It also does happen as sometimes someone adds a customer because he spelled it differently than the one in the database and thinks that it does not exist yet.
– Izzy
Nov 19 at 22:09
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can use a window function to solve this
WITH CTE AS
(
SELECT T1.*,
MIN(T1.CustomerId) OVER(PARTITION BY T1.FirstName, T1.LastName ORDER BY T1.CustomerId) RN
FROM T1
)
UPDATE T2
SET T2.CustomerId = CTE.RN
FROM T2 INNER JOIN CTE ON T2.CustomerId = CTE.CustomerId;
Demo
But in the first place you need to fix this, the customer should have one(1) Unique id, not multiple IDs.
Thank you. What I wanted to know was if SQL Server has built in that the records in the other tables that relate to this record can automatically be updated to the record it was merged into without specifically updating the CustomerId in the record in T2.
– Izzy
Nov 19 at 22:07
It also does happen as sometimes someone adds a customer because he spelled it differently than the one in the database and thinks that it does not exist yet.
– Izzy
Nov 19 at 22:09
add a comment |
up vote
0
down vote
You can use a window function to solve this
WITH CTE AS
(
SELECT T1.*,
MIN(T1.CustomerId) OVER(PARTITION BY T1.FirstName, T1.LastName ORDER BY T1.CustomerId) RN
FROM T1
)
UPDATE T2
SET T2.CustomerId = CTE.RN
FROM T2 INNER JOIN CTE ON T2.CustomerId = CTE.CustomerId;
Demo
But in the first place you need to fix this, the customer should have one(1) Unique id, not multiple IDs.
Thank you. What I wanted to know was if SQL Server has built in that the records in the other tables that relate to this record can automatically be updated to the record it was merged into without specifically updating the CustomerId in the record in T2.
– Izzy
Nov 19 at 22:07
It also does happen as sometimes someone adds a customer because he spelled it differently than the one in the database and thinks that it does not exist yet.
– Izzy
Nov 19 at 22:09
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use a window function to solve this
WITH CTE AS
(
SELECT T1.*,
MIN(T1.CustomerId) OVER(PARTITION BY T1.FirstName, T1.LastName ORDER BY T1.CustomerId) RN
FROM T1
)
UPDATE T2
SET T2.CustomerId = CTE.RN
FROM T2 INNER JOIN CTE ON T2.CustomerId = CTE.CustomerId;
Demo
But in the first place you need to fix this, the customer should have one(1) Unique id, not multiple IDs.
You can use a window function to solve this
WITH CTE AS
(
SELECT T1.*,
MIN(T1.CustomerId) OVER(PARTITION BY T1.FirstName, T1.LastName ORDER BY T1.CustomerId) RN
FROM T1
)
UPDATE T2
SET T2.CustomerId = CTE.RN
FROM T2 INNER JOIN CTE ON T2.CustomerId = CTE.CustomerId;
Demo
But in the first place you need to fix this, the customer should have one(1) Unique id, not multiple IDs.
edited Nov 18 at 21:17
answered Nov 18 at 21:12
Sami
6,61531038
6,61531038
Thank you. What I wanted to know was if SQL Server has built in that the records in the other tables that relate to this record can automatically be updated to the record it was merged into without specifically updating the CustomerId in the record in T2.
– Izzy
Nov 19 at 22:07
It also does happen as sometimes someone adds a customer because he spelled it differently than the one in the database and thinks that it does not exist yet.
– Izzy
Nov 19 at 22:09
add a comment |
Thank you. What I wanted to know was if SQL Server has built in that the records in the other tables that relate to this record can automatically be updated to the record it was merged into without specifically updating the CustomerId in the record in T2.
– Izzy
Nov 19 at 22:07
It also does happen as sometimes someone adds a customer because he spelled it differently than the one in the database and thinks that it does not exist yet.
– Izzy
Nov 19 at 22:09
Thank you. What I wanted to know was if SQL Server has built in that the records in the other tables that relate to this record can automatically be updated to the record it was merged into without specifically updating the CustomerId in the record in T2.
– Izzy
Nov 19 at 22:07
Thank you. What I wanted to know was if SQL Server has built in that the records in the other tables that relate to this record can automatically be updated to the record it was merged into without specifically updating the CustomerId in the record in T2.
– Izzy
Nov 19 at 22:07
It also does happen as sometimes someone adds a customer because he spelled it differently than the one in the database and thinks that it does not exist yet.
– Izzy
Nov 19 at 22:09
It also does happen as sometimes someone adds a customer because he spelled it differently than the one in the database and thinks that it does not exist yet.
– Izzy
Nov 19 at 22:09
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364826%2fsql-server-merging-one-record-to-another-and-changing-items-referencing-the-fi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Sample data and expected results as formatted text please not images.
– Sami
Nov 18 at 20:02
1
The short answer is NO... Nothing that would work automatically. You'd have to find all FK references to "CustomerID" (hopefully the database in question actually has good DRI) and generate commands to update their values.
– Jason A. Long
Nov 18 at 21:13