How to remove duplicates from table using SQL query

后端 未结 7 1644
遇见更好的自我
遇见更好的自我 2020-12-17 04:34

I have a table which is as follows:

emp_name   emp_address  sex  matial_status  
uuuu       eee          m    s
iiii       iii          f    s
uuuu       eee         


        
7条回答
  •  余生分开走
    2020-12-17 05:34

    It looks like all four column values are duplicated so you can do this -

    select distinct emp_name, emp_address, sex, marital_status
    from YourTable
    

    However if marital status can be different and you have some other column based on which to choose (for eg you want latest record based on a column create_date) you can do this

    select emp_name, emp_address, sex, marital_status
    from YourTable a
    where not exists (select 1 
                       from YourTable b
                      where b.emp_name = a.emp_name and
                            b.emp_address = a.emp_address and
                            b.sex = a.sex and
                            b.create_date >= a.create_date)
    

提交回复
热议问题