sql 表中删除字段重复的行

时间:2022-05-16 03:30:46

Id    Email    UserName
1    Taiseer.Joudeh@hotmail.com    TaiseerJoudeh
2    Hasan.Ahmad@mymail.com    HasanAhmad
3    Moatasem.Ahmad@outlook.com    MoatasemAhmad
4    Salma.Tamer@outlook.com    SalmaTamer
5    Ahmad.Radi@gmail.com    AhmadRadi
6    Bill.Gates@yahoo.com    BillGates
7    Shareef.Khaled@gmail.com    ShareefKhaled
8    Aram.Naser@gmail.com    AramNaser
9    Layla.Ibrahim@mymail.com    LaylaIbrahim
10    Rema.Oday@hotmail.com    RemaOday
11    Fikri.Husein@gmail.com    FikriHusein
12    Zakari.Husein@outlook.com    ZakariHusein
13    Taher.Waleed@mymail.com    LaylaIbrahim
14    Tamer.Wesam@yahoo.com    TamerWesam
15    Khaled.Hasaan@gmail.com    KhaledHasaan
16    Asaad.Ibrahim@hotmail.com    AsaadIbrahim
17    Tareq.Nassar@outlook.com    TareqNassar
18    Diana.Lutfi@outlook.com    DianaLutfi
19    Tamara.Malek@gmail.com    TamaraMalek
20    Arwa.Kamal@yahoo.com    ArwaKamal
21    Jana.Ahmad@yahoo.com    SalmaTamer
22    Nisreen.Tamer@gmail.com    NisreenTamer
23    Noura.Ahmad@outlook.com    SalmaTamer

去掉重复的UserName

delete Student2 where Id in

(
    select id from (
      select Id, UserName,ROW_NUMBER() over (partition by UserName order by id) orderid from Student2
    )t1
    where t1.orderid>1
)

delete from Students2 where id not in (

  select min(id) from Students2 group by UserName

)