数据结构是这个样子的:
ID      Date1                   name    date2                   re
10 2010-11-22 16:43:31 A44 2010-11-23 15:53:48 国际大方大方好
11 2010-11-22 16:40:30 A33 2010-11-23 15:59:53 不愿意审批
12 2010-11-22 16:40:30 A33 2010-11-23 16:03:00 0000000000000
13 2010-11-30 9:44:37 SS1 2010-11-30 9:59:40 111111111111
14 2010-11-30 9:42:54 SS2 2010-11-30 10:37:04 2222222222222
15 2010-11-30 9:46:22 SS3 2010-11-30 10:56:59 3333333333333
16 2010-11-22 16:40:30 A33 2010-11-23 15:48:10 "11111111111
17 2010-11-22 16:40:30 A33 2010-11-23 15:51:21 12341234
需要的数据是:
10 2010-11-22 16:43:31 A44 2010-11-23 15:53:48 国际大方大方
12 2010-11-22 16:40:30 A33 2010-11-23 16:03:00 0000000000000
13 2010-11-30 9:44:37 SS1 2010-11-30 9:59:40 11111111111
14 2010-11-30 9:42:54 SS2 2010-11-30 10:37:04 22222222222
15 2010-11-30 9:46:22 SS3 2010-11-30 10:56:59 333333333333
选择时间最大的重复姓名的的数据.

解决方案 »

  1.   


    select ID,Date1,name,date2,re
    from (select ID,Date1,name,date2,re,row_number() over(partition by name order by date2) rn
    from tb)
    where rn=1
      

  2.   

    --前面是时间最少的--这个是时间最大的
    select ID,Date1,name,date2,re
    from (select ID,Date1,name,date2,re,row_number() over(partition by name order by date2 desc) rn
    from tb)
    where rn=1
      

  3.   


    select * from tb t
    where date2=(select max(date2) from tb where t.name=name);
      

  4.   

    with tb as(
    select 10 ID,to_date('2010-11-22 16:43:31','yyyy-mm-dd hh24:mi:ss') Date1,'A44' name,
           to_date('2010-11-23 15:53:48','yyyy-mm-dd hh24:mi:ss') date2,'国际大方大方好' re
           from dual union all
    select 11,to_date('2010-11-22 16:40:30','yyyy-mm-dd hh24:mi:ss'),'A33',
           to_date('2010-11-23 15:59:53','yyyy-mm-dd hh24:mi:ss'),'不愿意审批' from dual union all
    select 12,to_date('2010-11-22 16:40:30','yyyy-mm-dd hh24:mi:ss'),'A33',
           to_date('2010-11-23 16:03:00','yyyy-mm-dd hh24:mi:ss'),'0000000000000' from dual union all
    select 13,to_date('2010-11-30 9:44:37','yyyy-mm-dd hh24:mi:ss'),'SS1',
           to_date('2010-11-30 9:59:40','yyyy-mm-dd hh24:mi:ss'),'111111111111' from dual union all
    select 14,to_date('2010-11-30 9:42:54','yyyy-mm-dd hh24:mi:ss'),'SS2',
           to_date('2010-11-30 10:37:04','yyyy-mm-dd hh24:mi:ss'),'2222222222222' from dual union all
    select 15,to_date('2010-11-30 9:46:22','yyyy-mm-dd hh24:mi:ss'),'SS3',
           to_date('2010-11-30 10:56:59','yyyy-mm-dd hh24:mi:ss'),'3333333333333' from dual union all
    select 16,to_date('2010-11-22 16:40:30','yyyy-mm-dd hh24:mi:ss'),'A33',
           to_date('2010-11-23 15:48:10','yyyy-mm-dd hh24:mi:ss'),'11111111111' from dual union all
    select 17,to_date('2010-11-22 16:40:30','yyyy-mm-dd hh24:mi:ss'),'A33',
           to_date('2010-11-23 15:51:21','yyyy-mm-dd hh24:mi:ss'),'12341234' from dual)
    --以上为提供数据的语句
    select ID,Date1,name,date2,re
    from (
    select ID,Date1,name,date2,re,row_number() over(partition by name order by date2 desc) rn
    from tb
    )
    where rn=1        ID DATE1          NAM DATE2          REMARK
    ---------- -------------- --- -------------- --------------
            12 22-11月-10     A33 23-11月-10     0000000000000
            10 22-11月-10     A44 23-11月-10     国际大方大方好
            13 30-11月-10     SS1 30-11月-10     111111111111
            14 30-11月-10     SS2 30-11月-10     2222222222222
            15 30-11月-10     SS3 30-11月-10     3333333333333