Table:
F1   F2
1    1,9
2    1,8
怎样检索出结果集中一条F2最长的记录

解决方案 »

  1.   

    比如:
    F1   F2
    1    1,9
    2    1,8,2
    上面检索出
    F1   F2
    2    1,8,2
      

  2.   

    不好意思,例子举错了,应该是
    比如:
    F1   F2
    1    1,9
    1    1,8,2
    2    1,9
    2    1,8,2
    2    1,3,2,1上面检索出
    F1   F2
    1    1,8,2
    2    1,3,2,1
      

  3.   

    select a.f1,a.f2
    (select f1,f2,f2.length as fn
    from tablename) a,(select f1,max(f2.length) as fn
    from tablename group by f1) b
    where a.fn=b.fn
    and a.f1=b.f1
      

  4.   

    select distinct first_value(a.f1)over(partition by a.f1 order by a.fn desc),first_value(a.f2)over(partition by a.f1 order by a.fn desc)
    from 
    (select f1,f2,f2.length as fn
    from tablename) a
      

  5.   

    select f1,max(f2) from test group by f1应该可以
      

  6.   

    用分析函数SELECT
    DISTINCT FIRST_VALUE(A.F1)OVER(PARTITION BY A.F1 ORDER BY A.LEN_F2 DESC) AS F1,
    FIRST_VALUE(A.F2) OVER(PARTITION BY A.F1 ORDER BY A.LEN_F2 DESC) AS F2
    FROM (SELECT F1,F2,LENGTH(F2) AS LEN_F2 FROM TT) A;如果是8i的话,就只能用 sbaz(万神渡劫) 提供的方法了:
    select a.f1,a.f2
    (select f1,f2,f2.length as fn
    from tablename) a,(select f1,max(f2.length) as fn
    from tablename group by f1) b
    where a.fn=b.fn
    and a.f1=b.f1
      

  7.   

    谢谢大家,我用树型结果集的level解决了这个问题.