October 22, 2008 at 7:33 pm
Hi,
As an example I have five rows of data as follows
Id Ind
_____ ____
123456 No
123456 Yes
123456 Yes
123456 No
123456 Yes
What I'm trying to do is write an update that checks if any row has an Ind = 'Yes' then apply that 'Yes to all the rows. So the result after would look like this:
Id Ind
_____ ____
123456 Yes
123456 Yes
123456 Yes
123456 Yes
123456 Yes
Any ideas?
October 22, 2008 at 8:03 pm
maybe something like this...
UPDATE A
SET A.Ind = B.Ind
FROM
[yourTable] A inner join
[yourTable] B ON A.Id = B.Id
WHERE
B.Ind = 'Yes'
October 22, 2008 at 8:08 pm
Here you go:
set nocount on
declare @yourtable table (id int, ind varchar(3))
insert into @yourtable values (1, 'No')
insert into @yourtable values (2, 'No')
insert into @yourtable values (3, 'Yes')
insert into @yourtable values (4, 'No')
insert into @yourtable values (5, 'Yes')
insert into @yourtable values (1, 'No')
insert into @yourtable values (3, 'Yes')
insert into @yourtable values (2, 'Yes')
insert into @yourtable values (2, 'Yes')
select * from @yourtable
update y
set y.ind = 'Yes'
from @yourtable y
where exists (select id from @yourtable z where z.id = y.id and ind = 'Yes')
select * from @yourtable order by id, ind
Happy coding!
-- CK
October 24, 2008 at 4:45 am
[font="Verdana"]
Refer below URL:
http://www.sqlservercentral.com/Forums/Topic590618-145-1.aspx
Mahesh
[/font]
MH-09-AM-8694
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply