现有一张数据表
Name  Time          Type
AAA   2009-12-25    1
BBB   2009-12-25    1
BBB   2009-12-26    3
CCC   2009-12-25    1
CCC   2009-12-24    2
CCC   2009-12-26    5
需要的结果是(重名的只要保留type字段值最大的那条记录)Name  Time          Type
AAA   2009-12-25    1
BBB   2009-12-26    3
CCC   2009-12-26    5

解决方案 »

  1.   

    SELECT * FROM (select Name,Time,Type,row_number() over (partition by name order by Type DESC ) RN FROM T ) TEMP WHERE TEMP.RN=1
      

  2.   

    select a.Name,a.Time,A.Type
    from tab a,(select name,max(type) as type from tab group by name) b
    where a.Name=b.Name and A.Type=b.Type
      

  3.   

    select t.* from tb t where type = (select max(type) from tb where Name = t.Name) order by t.Nameselect t.* from tb t where not exists (select 1 from tb where Name = t.Name and type > t.type) order by t.Name
      

  4.   

    --以下语句除建表语句不一样外,其他和oracle一样.
    create table tb(Name varchar(10), Time datetime, Type int)
    insert into tb values('AAA' , '2009-12-25' , 1 )
    insert into tb values('BBB' , '2009-12-25' , 1 )
    insert into tb values('BBB' , '2009-12-26' , 3 )
    insert into tb values('CCC' , '2009-12-25' , 1 )
    insert into tb values('CCC' , '2009-12-24' , 2 )
    insert into tb values('CCC' , '2009-12-26' , 5 )
    goselect t.* from tb t where type = (select max(type) from tb where Name = t.Name) order by t.Nameselect t.* from tb t where not exists (select 1 from tb where Name = t.Name and type > t.type) order by t.Namedrop table tb/*
    Name       Time                                                   Type        
    ---------- ------------------------------------------------------ ----------- 
    AAA        2009-12-25 00:00:00.000                                1
    BBB        2009-12-26 00:00:00.000                                3
    CCC        2009-12-26 00:00:00.000                                5(所影响的行数为 3 行)Name       Time                                                   Type        
    ---------- ------------------------------------------------------ ----------- 
    AAA        2009-12-25 00:00:00.000                                1
    BBB        2009-12-26 00:00:00.000                                3
    CCC        2009-12-26 00:00:00.000                                5(所影响的行数为 3 行)*/
      

  5.   

    select *
    from 一张数据表 a
    where not exists (
    select 1 from 一张数据表 
    where Name = a.Name 
    and type > a.type)
      

  6.   

    with temp as(
    select 'AAA' name,to_date('2009-12-25','yyyy-mm-dd') time, 1 type from dual
    union all
    select 'BBB' name,to_date('2009-12-25','yyyy-mm-dd') time, 1 type from dual
    union all
    select 'BBB' name,to_date('2009-12-26','yyyy-mm-dd') time, 3 type from dual
    union all
    select 'CCC' name,to_date('2009-12-25','yyyy-mm-dd') time, 1 type from dual
    union all
    select 'CCC' name,to_date('2009-12-24','yyyy-mm-dd') time, 2 type from dual
    union all
    select 'CCC' name,to_date('2009-12-26','yyyy-mm-dd') time, 5 type from dual
    )
    select * from(
    select name,time,type,row_number()over(partition by name order by type desc) rn from temp
    ) where rn = 1
      

  7.   

    select name,
      max(time)keep(dense_rank last order by type),
      max(type)
    from table1
    group by name如果type不会重复的话,也可以用
    not exists或row_number()来处理
      

  8.   

    7楼的方案比较好,楼上的大多虽然能满足楼主要求,但如果表是以下情况就没法满足了
    Name  Time          Type
    AAA  2009-12-25    1
    AAA  2009-12-25    1
    BBB  2009-12-25    1
    BBB  2009-12-26    3
    CCC  2009-12-25    1
    CCC  2009-12-24    2
    CCC  2009-12-26    5 
      

  9.   

    SELECT * FROM (select Name,Time,Type,row_number() over (partition by name order by Type DESC ) RN FROM T ) TEMP WHERE TEMP.RN=1
      

  10.   

    SELECT * FROM (select Name,Time,Type,row_number() over (partition by name order by Type DESC ) RN FROM T ) TEMP WHERE TEMP.RN=1
      

  11.   

    select a.*
      from (
                select t.*,row_number() over(partition by name order by type desc) RN
                   from table_a t
               ) a
      where a.RN=1