Updating Rows with same values

  • 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?

  • 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'

  • 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

  • [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