表名:test1name     fl     money
1111     99      1
1111     99      2
1111     96      1
1111     96      1
2222     99      2
2222     98      1得到的结果是name     fl     money
1111     96     2
2222     98     1
条件是:
1、fl要最小的,每个相同名称只要一条。
2、money累加希望得到大家的帮助写出查询语句。在此感谢。

解决方案 »

  1.   

    不容易,这都让我推出来了。
    select name,fl,sum(money) from test1 where (name,fl) in (select name,min(fl) from test1 GROUP BY name) GROUP BY name;
      

  2.   

    select T2.name,T2.f1,sum(T2.miney) 
    from (
    select name,min(f1) as minF
    from test1 A
    group by name
    )T1,test1 T2
    where T1.name = T2.name and T1.minF=T2.f1
    group by T2.name,T2.f1
      

  3.   


    select t1.name,t1.f1,sum(t1.money) from (select * from test1 order by f1) as t1 grou by t1.name
      

  4.   


    select t1.name,t1.f1,sum(t1.money) from (select * from test1 order by f1) as t1 group by t1.name
      

  5.   

    楼上不对。
    应该这样:select a.name,a.f1,sum(money) from t1 a, (select name, min(f1) f1 from t1 group by name) b
        where a.name = b.name and a.f1 = b.f1 group by a.name;
      

  6.   

    又研究了一种方法,在某些条件下速度可能会快点儿。
    select t1.name,min(f1),t1.money from (select name,f1,sum(money) money from t1 group by name,f1) as t1 group by t1.name
      

  7.   

    select name,fl,sum(money) from tt a 
    where not exists(select 1 from tt where a.name=name and a.money>money)
      

  8.   

    还有这种条件搜索的知道了1111这个名字。找出下面结果。得到的结果是name fl money
    1111 96 2
      

  9.   

    select name,fl,sum(money) from tt a  
    where not exists(select 1 from tt where a.name=name and a.money>money) AND A.NAME='1111'
      

  10.   

    参考下贴中的多种方法http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html
    [征集]分组取最大N条记录方法征集,及散分....