select name, max(age) from users group by name or select a.Id, a.name , a.age , a.others 
from users a , (select name, max(age) from users group by name ) b 
where a.name = b.name and a.age = b.age

解决方案 »

  1.   

    第一个答案,无法得到包括ID在内的其他字段信息
    第二个,name和age联合也不一定唯一啊,ID是主键来得
      

  2.   

    这样满足你的要求么
    select id,name, max(age),others
    from users 
    group by id,name,others
      

  3.   

    谢谢支持先,ID是主键,如果按ID分组的话,其他分组也不在有意义了
      

  4.   

    表user结构:
    ID | name|  age | others.....
    name有重复项,Id为主键,age为number,others为说明字段
    求一查询
    1)Name不能重复,
    2)如果Name有重复去年龄最大的一条数据
    3)要求能查到,包括ID和others其他字段
      

  5.   

    对于一楼第二个答案的话,如果有两条记录,他们的name,age都相同的话,那这两条记录都会被查出来,但是我只要一条
      

  6.   

    这样看看。select distinct name, max(age) from users group by name
      

  7.   

    --下面語句絕對沒錯,樓主可以直接拷入SQL*Plus中運行:
    select (select id from user where name=a.name and age=max(a.age)) as id,name
    ,max(age) as age,
    (select others from user where name=a.name and age=max(a.age)) as others
     from user a group by name;
      

  8.   

    --或者使用如下語句也絕對符合要求,但要注意,它所查出來的結果和我上面給的語句的結果是一樣
    --的,但結果集的排序剛好相反:
    select a.ID,a.name,a.age,a.others 
    from users a JOIN (select name,max(age) from users group by name ) b 
    ON a.name = b.name and a.age = b.age;
    --上面語句實際上和一樓給的第二條語句是一樣的意思,呵呵!
      

  9.   

    另外對於樓主所說的,因為可能出現name和age同時相同的行且此行又是同name中的最大值行,所以上面兩條語句再作一點小的修改,就無論如何都符合樓主的要求了:
    -- 一、
    select (select max(id) from user where name=a.name and age=max(a.age)) as id,name
    ,max(age) as age,
    (select max(others) from user where name=a.name and age=max(a.age)) as others
     from user a group by name;
    -- 二、
    select max(a.ID) ID,a.name,a.age,Max(a.others) others 
    from users a JOIN (select name,max(age) from users group by name ) b 
    ON a.name = b.name and a.age = b.age;
    group by a.name,a.age;