How to get one unique record from the same list of records from table? No Unique constraint in the table

后端 未结 2 1708
花落未央
花落未央 2021-01-16 16:19

I have one query in SQL Server output,

Suppose i have one table (Ex.StudentMaster) having some fields-No unique constraints. For Ex. RollNumber and Name The table h

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-16 17:19

    Any row is a third row :-)

    create table test
    (
    n int,
    name varchar(30)
    );
    
    insert into test values(1,'yoko'),(1,'yoko'),(1,'yoko');
    
    select ROW_NUMBER() over(order by name) as ordinal, * from test;
    

    Deleting the "third" row :-)

    with a as
    (
    select ROW_NUMBER() over(order by name) as ordinal, * from test
    )
    delete from a where a.ordinal = 3
    

    Deleting the last row:

    with a as
    (
    select ROW_NUMBER() over(order by name) as ordinal, * from test
    )
    delete from a where a.ordinal = (select MAX(ordinal) from a)
    

提交回复
热议问题