zoukankan      html  css  js  c++  java
  • SQL去重复记录,留一条符合条件记录 宁静以致远

    declare @tbl table(name varchar(20),age datetime)
    insert into @tbl 
    select 'jacky','1985-12-23' union 
    select 'myjacky','1986-12-10' union
    select 'myjacky','1986-12-12' union
    select 'jacky','1988-09-23'
    
    --方法一
    select a.* from @tbl a 
    inner join (select max(age) as age,name from @tbl group by name) b
    on a.name=b.name and a.age=b.age
    
    --方法二
    select a.* from @tbl a where 1>(select count(*) from @tbl where name = a.name and age > a.age) order by a.name
    
    --方法三
    select a.* from @tbl a where not exists(select 1 from @tbl where name = a.name and age < a.age)
    
    --方法四
    select a.* from @tbl a where age = (select min(age) from @tbl where name = a.name) order by a.name
  • 相关阅读:
    wordpress建个人博客
    函数(一)
    字符串格式化
    集合运算
    基本数据类型(二)
    基本数据类型(一)
    分享一个下片神器
    Proxyee
    基本运算符
    条件语句和while循环
  • 原文地址:https://www.cnblogs.com/myjacky/p/2522837.html
Copyright © 2011-2022 走看看