Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,626 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have data like below,
create table #tbl (Name varchar(20), Class varchar(20))
insert into #tbl values ('A', 'class1'), ('A', 'class2'), ('B', 'class1'), ('C', 'class2'), ('D', 'class3'), ('D', 'class1'), ('E', 'class4')
select * from #tbl
drop table #tbl
I need to update class of "B" with value "XX" which has 1 items with class1, rest I need to ignore.
Expected output should be,
;With cte As
(Select Name, Class, Count(*) Over(Partition By Name) As Cnt
From #tbl)
Update cte
Set Class = 'XX'
Where Class = 'class1' And Cnt = 1;
Tom