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

后端 未结 2 1710
花落未央
花落未央 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:04

    You can use DISTINCT which return's distinct combination's of columns.

    SELECT DISTINCT RollNo, Name
    FROM mytable
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题