有如下表:表1 
ID Name Date          NO
1 王小琴 2008年07月25日 1
2 黎英 2004年11月11日 1
3 郝洋 2006年12月22日 10
4 康恺 2005年07月04日 7
5 申庆成 2006年12月22日 10
6 李保安 2008年08月19日 6
7 刘忠华 2008年10月28日 8
8 赵新宇 2008年06月02日 3
9 侯松滨 2009年08月04日 5
10 黄福成 2008年07月24日 1其中ID是唯一的,现在要做的就是 只留一个NO 当两个NO相同的时候留下Date比较大的,
ID相同并且Date也相同的时候随机留一个就行

解决方案 »

  1.   

    --直接
    select * 
    from tb t 
    where id=(select max(ID) from tb where no=t.no)
      

  2.   

    select * 
    from tb t 
    where not exists(select 1  from tb where no=t.no and id>t.id)
      

  3.   

    select t.* from t where Date = (select max(Date) from tb where no = t.no)
    select t.* from t where not exists (select 1 from tb where no = t.no and Date > t.Date)
      

  4.   

    [code=SQL]select * 
    from tb t 
    where id=(select max(id) from tb where no=t.no)select * 
    from tb t
    where id=(select top 1 id from tb where no=t.no order by id desc)select * 
    from tb t 
    where not exists(select 1  from tb where no=t.no and id>t.id)三选一都行[/code]
      

  5.   

    其中ID是唯一的?
    ID相同并且Date也相同?这是不是矛盾了?
      

  6.   

    不好意思,写错了
    是NO相同并且Date也相同的时候随机留一个就行
      

  7.   

    create table tb(ID int,Name varchar(10),Date varchar(20),NO int)
    insert into tb values(1 , '王小琴' ,'2008年07月25日', 1)
    insert into tb values(2 , '黎英'   ,'2004年11月11日', 1)
    insert into tb values(3 , '郝洋'   ,'2006年12月22日', 10)
    insert into tb values(4 , '康恺'   ,'2005年07月04日', 7)
    insert into tb values(5 , '申庆成' ,'2006年12月22日', 10)
    insert into tb values(6 , '李保安' ,'2008年08月19日', 6)
    insert into tb values(7 , '刘忠华' ,'2008年10月28日', 8)
    insert into tb values(8 , '赵新宇' ,'2008年06月02日', 3)
    insert into tb values(9 , '侯松滨' ,'2009年08月04日', 5)
    insert into tb values(10, '黄福成' ,'2008年07月24日', 1)
    go--相同NO时取大的date
    select t.* from tb t where Date = (select max(Date) from tb where no = t.no) order by noselect t.* from tb t where not exists (select 1 from tb where no = t.no and Date > t.Date) order by no
    /*
    ID          Name       Date                 NO          
    ----------- ---------- -------------------- ----------- 
    1           王小琴        2008年07月25日          1
    8           赵新宇        2008年06月02日          3
    9           侯松滨        2009年08月04日          5
    6           李保安        2008年08月19日          6
    4           康恺         2005年07月04日          7
    7           刘忠华        2008年10月28日          8
    3           郝洋         2006年12月22日          10
    5           申庆成        2006年12月22日          10(所影响的行数为 8 行)
    */
    --相同NO相同date时取大的ID
    select m.* from
    (
      select t.* from tb t where Date = (select max(Date) from tb where no = t.no)
    ) m where id = (select min(id) from 
    (
      select t.* from tb t where Date = (select max(Date) from tb where no = t.no)
    ) n where no = m.no and date = m.date)
    order by m.noselect m.* from
    (
      select t.* from tb t where not exists (select 1 from tb where no = t.no and Date > t.Date)
    ) m where id = (select min(id) from 
    (
      select t.* from tb t where not exists (select 1 from tb where no = t.no and Date > t.Date)
    ) n where no = m.no and date = m.date)
    order by m.no
    /*
    ID          Name       Date                 NO          
    ----------- ---------- -------------------- ----------- 
    1           王小琴        2008年07月25日          1
    8           赵新宇        2008年06月02日          3
    9           侯松滨        2009年08月04日          5
    6           李保安        2008年08月19日          6
    4           康恺         2005年07月04日          7
    7           刘忠华        2008年10月28日          8
    3           郝洋         2006年12月22日          10(所影响的行数为 7 行)
    */drop table tb
      

  8.   


    select ID,Name,Date,NO
    from (select *,row_number() over (partition by no order by date desc) rid from tb) t
    where rid=1delete t from (select *,row_number() over (partition by no order by date desc) rid from tb) t 
    where rid>1