December 3, 2018 at 6:46 am
Hi,
I have a table that holds data where for some of the identical IDs the names are different . One real name and the other comes as Generic .I want to update the row with the person's real name where IDS are same and Name is Generic
CREATE TABLE #Exceptions
(
ID INT,
Names VARCHAR(100)
)
INSERT INTO #Exceptions ( ID,Names )
SELECT 1,'Generic'
UNION ALL
SELECT 1, 'John Kelley'
UNION ALL
SELECT 2, 'Hello World'
UNION ALL
SELECT 3, 'Hello World2'
UNION ALL
SELECT 3, 'Generic'
SELECT * FROm
#Exceptions
--Correct Result
SELECT 1 AS ID,'John Kelley' AS Names
UNION ALL
SELECT 1 AS ID, 'John Kelley' AS Names
UNION ALL
SELECT 2 AS ID, 'Hello World' AS Names
UNION ALL
SELECT 3 AS ID, 'Hello World2' AS Names
UNION ALL
SELECT 3 AS ID, 'Hello World2' AS Names
DROP TABLE #Exceptions
Thanks,
PSB
December 3, 2018 at 7:05 am
Something like this?
UPDATE wrong
SET names = correct.names
FROM
#exceptions wrong
JOIN #exceptions correct ON wrong.Id = correct.id
WHERE
wrong.Names = 'Generic'
AND correct.Names <> 'Generic';
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
December 3, 2018 at 7:27 am
PSB - Monday, December 3, 2018 6:46 AMHi,I have a table that holds data where for some of the identical IDs the names are different . One real name and the other comes as Generic .I want to update the row with the person's real name where IDS are same and Name is Generic
CREATE TABLE #Exceptions
(
ID INT,
Names VARCHAR(100)
)
INSERT INTO #Exceptions ( ID,Names )
SELECT 1,'Generic'
UNION ALL
SELECT 1, 'John Kelley'
UNION ALL
SELECT 2, 'Hello World'
UNION ALL
SELECT 3, 'Hello World2'
UNION ALL
SELECT 3, 'Generic'
SELECT * FROm
#Exceptions
--Correct Result
SELECT 1 AS ID,'John Kelley' AS Names
UNION ALL
SELECT 1 AS ID, 'John Kelley' AS Names
UNION ALL
SELECT 2 AS ID, 'Hello World' AS Names
UNION ALL
SELECT 3 AS ID, 'Hello World2' AS Names
UNION ALL
SELECT 3 AS ID, 'Hello World2' AS Names
DROP TABLE #Exceptions
Thanks,
PSB
May be this
update exp1 set exp1.id=exp1.id,exp1.names=(
case when exp1.id=exp2.id and exp1.names=exp2.names then exp1.names else
exp2.names end)
from #Exceptions exp1 inner join #Exceptions exp2
on exp1.id=exp2.id
where exp2.names<>'Generic'
Saravanan
December 3, 2018 at 7:27 am
PSB - Monday, December 3, 2018 6:46 AMHi,I have a table that holds data where for some of the identical IDs the names are different . One real name and the other comes as Generic .I want to update the row with the person's real name where IDS are same and Name is Generic
CREATE TABLE #Exceptions
(
ID INT,
Names VARCHAR(100)
)
INSERT INTO #Exceptions ( ID,Names )
SELECT 1,'Generic'
UNION ALL
SELECT 1, 'John Kelley'
UNION ALL
SELECT 2, 'Hello World'
UNION ALL
SELECT 3, 'Hello World2'
UNION ALL
SELECT 3, 'Generic'
SELECT * FROm
#Exceptions
--Correct Result
SELECT 1 AS ID,'John Kelley' AS Names
UNION ALL
SELECT 1 AS ID, 'John Kelley' AS Names
UNION ALL
SELECT 2 AS ID, 'Hello World' AS Names
UNION ALL
SELECT 3 AS ID, 'Hello World2' AS Names
UNION ALL
SELECT 3 AS ID, 'Hello World2' AS Names
DROP TABLE #Exceptions
Thanks,
PSB
Really sorry system slow . deleted the post
Saravanan
December 3, 2018 at 7:27 am
PSB - Monday, December 3, 2018 6:46 AMHi,I have a table that holds data where for some of the identical IDs the names are different . One real name and the other comes as Generic .I want to update the row with the person's real name where IDS are same and Name is Generic
CREATE TABLE #Exceptions
(
ID INT,
Names VARCHAR(100)
)
INSERT INTO #Exceptions ( ID,Names )
SELECT 1,'Generic'
UNION ALL
SELECT 1, 'John Kelley'
UNION ALL
SELECT 2, 'Hello World'
UNION ALL
SELECT 3, 'Hello World2'
UNION ALL
SELECT 3, 'Generic'
SELECT * FROm
#Exceptions
--Correct Result
SELECT 1 AS ID,'John Kelley' AS Names
UNION ALL
SELECT 1 AS ID, 'John Kelley' AS Names
UNION ALL
SELECT 2 AS ID, 'Hello World' AS Names
UNION ALL
SELECT 3 AS ID, 'Hello World2' AS Names
UNION ALL
SELECT 3 AS ID, 'Hello World2' AS Names
DROP TABLE #Exceptions
Thanks,
PSB
Really sorry system slow . deleted the post
Saravanan
December 3, 2018 at 7:27 am
PSB - Monday, December 3, 2018 6:46 AMHi,I have a table that holds data where for some of the identical IDs the names are different . One real name and the other comes as Generic .I want to update the row with the person's real name where IDS are same and Name is Generic
CREATE TABLE #Exceptions
(
ID INT,
Names VARCHAR(100)
)
INSERT INTO #Exceptions ( ID,Names )
SELECT 1,'Generic'
UNION ALL
SELECT 1, 'John Kelley'
UNION ALL
SELECT 2, 'Hello World'
UNION ALL
SELECT 3, 'Hello World2'
UNION ALL
SELECT 3, 'Generic'
SELECT * FROm
#Exceptions
--Correct Result
SELECT 1 AS ID,'John Kelley' AS Names
UNION ALL
SELECT 1 AS ID, 'John Kelley' AS Names
UNION ALL
SELECT 2 AS ID, 'Hello World' AS Names
UNION ALL
SELECT 3 AS ID, 'Hello World2' AS Names
UNION ALL
SELECT 3 AS ID, 'Hello World2' AS Names
DROP TABLE #Exceptions
Thanks,
PSB
Really sorry system slow . deleted the post
Saravanan
December 3, 2018 at 7:27 am
PSB - Monday, December 3, 2018 6:46 AMHi,I have a table that holds data where for some of the identical IDs the names are different . One real name and the other comes as Generic .I want to update the row with the person's real name where IDS are same and Name is Generic
CREATE TABLE #Exceptions
(
ID INT,
Names VARCHAR(100)
)
INSERT INTO #Exceptions ( ID,Names )
SELECT 1,'Generic'
UNION ALL
SELECT 1, 'John Kelley'
UNION ALL
SELECT 2, 'Hello World'
UNION ALL
SELECT 3, 'Hello World2'
UNION ALL
SELECT 3, 'Generic'
SELECT * FROm
#Exceptions
--Correct Result
SELECT 1 AS ID,'John Kelley' AS Names
UNION ALL
SELECT 1 AS ID, 'John Kelley' AS Names
UNION ALL
SELECT 2 AS ID, 'Hello World' AS Names
UNION ALL
SELECT 3 AS ID, 'Hello World2' AS Names
UNION ALL
SELECT 3 AS ID, 'Hello World2' AS Names
DROP TABLE #Exceptions
Thanks,
PSB
Really sorry system slow . deleted the post
Saravanan
December 3, 2018 at 7:32 am
Hold the phone a minute... why do you have two tables that you must maintain the name in? That's not "normalization" and having to do such maintenance is contrary to just about every sane practice I'm aware of for databases.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 3, 2018 at 8:26 am
Thanks, Works perfectly.
December 3, 2018 at 11:25 am
It is one table .
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply