SQLTeam.com | Weblogs | Forums

Need help in sql query


#1

Query

Input Table:

Code Name RN
1 A 1
2 A 2
3 B 1
4 C 1
5 D 1
6 D 2

Need to update column Code when RN = 2

Output Table should like below:
Code Name RN
A A 1
A A 2
3 B 1
4 C 1
D D 1
D D 2

Thanks in advance


#2

create table #table (
Code varchar(255),
Name varchar(255),
RN int
)

insert into #table values
('1','A',1)
,('2','A',2)
,('3','B',1)
,('4','C',1)
,('5','D',1)
,('6','D',2)

--original
select * from #table

update A
set Code = Name
From #table as A
where Exists (
select *
from #table as B
where b.rn = 2 and A.Name = B.Name
)

--desired result
select * from #table


#3

Thank you.
It's working.


#4

:+1: would be great if you hit the like button :grinning: