You must Sign In to post a response.
  • Category: SQL Server

    Check duplicate names in SQL server table

    Hi all,
    i have below table like this.
    id name class
    1 A null
    2 B null
    3 c null
    4 d null
    1 A 9
    3 c 9

    how to check data with bit value is exist or not. i need below output.

    i tried this... but not working
    SELECT Id,name,class
    (case when isnull(class,123)=123 then 0 else 1 end) as exist
    FROM test_Table.

    i need below output
    id name class exist
    2 B null 0
    4 d null 0
    1 A 9 1
    3 c 9 1

    regards
    Kishore
  • #769355
    Hi Kishore,

    Please try below code, it results as per your expectations:

    SELECT Id,name,class,ROW_NUMBER() over ( order by id desc) row,
    (case when isnull(class,123)=123 then 0 else 1 end) as exist
    into #t
    FROM test_Table

    --select * from #t

    delete from #t
    where row not in
    (select max(row)
    from #t
    group by id,name)

    select Id,name,class,exist from #t
    drop table #t

    Thanks!
    Anjali Bansal

    ~Give your best and lead the world


  • Sign In to post your comments