表t(id,age);表中一共有3条数据,分别用a,b,c 来表示。如果a中的age大于b中的age就显示a数据否则显示b数据,如果b中age大于c中age就显示b数据否则显示c数据。用一条sql语句。等高手

解决方案 »

  1.   

    create table t(id int,age int)
    insert into t values(1,20) --a
    insert into t values(2,21) --b
    insert into t values(3,19) --c
    /*
    如果a中的age大于b中的age就显示a数据否则显示b数据,
    如果b中的age大于c中的age就显示b数据否则显示c数据。
    */
    select
        a.id,
        case when a.age>b.age then a.age else nvl(b.age,a.age) end as age
    from t as a 
        left join t as b 
          on a.id=b.id-1
      

  2.   

    id          age
    ----------- -----------
    1           21
    2           21
    3           19
      

  3.   

    不知道是不是取最大值的意思?如果结果只有一条记录,如下:1)
    select max(T_T_AGE)from t_t 2)
    select T_T_ID ,T_T_AGE,rownum
    from(
    select T_T_ID ,T_T_AGE
    from t_t 
    order by T_T_AGE desc)
    where rownum=1;
      

  4.   

    改一下数据:
    1)
    select max(AGE)from t2)
    select ID ,AGE,rownum
    from(
    select ID ,AGE
    from t
    order by AGE desc)
    where rownum=1;
      

  5.   

    这样的话,题目的意思就是省略掉最小的age了,解法:
    select T_T_ID ,T_T_AGE
    from t_t 
    where T_T_AGE not in
    (
    select T_T_AGE
    from t_t 
    where rownum=1
    );
      

  6.   

    改一下数据: 
    select ID ,AGE
    from t
    where AGE not in
    (
    select AGE
    from t
    where rownum=1
    );
      

  7.   

    也不是这样的啊
    想想  如果b比a和c都大的话   那就要将b输出两遍了 对不
    我的意见是:在如下表中:
    id          age 
    ----------- ----------- 
    1          18 
    2          21 
    3          19 
    select id,max(age) from 表 where id='1' or id='2'
    nuion
    select id,max(age) from 表 where id='2' or id='3'
    因为表的记录很少  这样应该可以吧!
      

  8.   

    谢谢你们,你们的2种方法都能达到目的。但是我觉得这不是这题目的本意。它的本意是把条件逻辑体现在sql一句中。
    至于数据相等的问题,题目种没有给出条件,既然没有给那我想就可以随便处理了(也 可以当做数据不相等来处理)。