How to merge when one field has two values

  • I have a table Item

    Create table #Item ( Item varchar(40), IsNew varchar(1))

    Insert into #Item values ('ABC','N')

    Insert into #Item values ('ABC','Y')

    Insert into #Item values ('DEF','Y')

    Insert into #Item values ('GHI','N')

    Insert into #Item values ('JKL','N')

    Insert into #Item values ('JKL','Y')

    When I run for the table I have, (N and Y means No and Yes)

    ABCN

    ABCY

    DEFY

    GHIN

    JKLN

    JKLY

    We can see ABC and JKL items have ‘Yes’ and ‘No’. So when one Item has both ‘Yes’ and ‘No’ then I need to change it to ‘No’. So I should get result like

    ABCN

    DEFY

    GHIN

    JKLN

    Can you please help me?

  • Will this do?

    select ITEM, CASE WHEN COUNT(ITEM) > 1 THEN 'N' ELSE 'Y' END

    from #item

    GROUP BY ITEM

  • thank you, that worked.

  • No problem!

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply