数据库例如:
id       姓  名   奖  金
1         张三     20万
2         李四     10万
3         王一     5万
4         王一     50万
5         李四     6万
我要实现下面的效果
id       姓  名   奖  金
4        王一      55万 查出谁的奖金总合是最高的,SQL语句应该怎么写?

解决方案 »

  1.   

    select *
    from(
    select id,姓名,sum(奖金)
    from table
    group by id,姓名
    order bv 3 desc)
    where rownum=1
      

  2.   

    select * 
    from( 
    select max(id),姓名,sum(奖金) 
    from table 
    group by 姓名)
    感觉你的ID没有什么用处啊
      

  3.   

    我觉得以上的结果好像都不完全正确,你可以用下面的试试:
    ---建表:
     create table t_reward (id varchar2(3) primary key ,name varchar2(20),rewards number(10,2));--插入数据,此处不在写。
    --执行sql语句:
    SELECT id, NAME, max_reward
    FROM (SELECT id,
                 NAME,
                 rewards,
                 SUM(rewards) over(PARTITION BY NAME ORDER BY NAME) max_reward
          FROM t_reward
          ORDER BY max_reward DESC, id DESC) b
    WHERE rownum = 1;
    --得到你想要的结果
      

  4.   

    select id,姓名,奖金
    from(
    select min(id)id,姓名,sum(奖金)奖金,
      dense_rank()over(order by sum(奖金)desc)dk
    from table1
    group by 姓名)
    where dk=1hebo的就差不多了,疏忽了id而已
      

  5.   

    SQL> select * from tt;        ID NAME                                          BONUS
    ---------- ---------------------------------------- ----------
             1 wh                                             1000
             2 wp                                             2000
             3 wr                                             2500
             1 wh                                             2500
    SQL> select t2.id,t2.name,t2.total from(
      2     select t1.*,rownum rn from
      3             (select id,name,sum(bonus) total
      4             from tt
      5             group by id,name
      6             order by total desc) t1) t2
      7  where rn=1;        ID NAME                                          TOTAL
    ---------- ---------------------------------------- ----------
             1 wh                                             3500
      

  6.   

    恩 狂浪的好像好点  只用了两层就可以了 呵呵
    SQL> select id,name,bonus
      2  from(
      3  select min(id) id,name,sum(bonus) bonus,
      4  dense_rank() over(order by sum(bonus) desc) dk
      5  from tt
      6  group by name)
      7  where dk=1;        ID NAME                                          BONUS
    ---------- ---------------------------------------- ----------
             1 wh                                             3500
      

  7.   


    我用的是oracle 8i 数据库,测试了一下 提示少了右括号 不知道问题出在哪里,是不是数据库版本原因 不支持某个函数的用法
      

  8.   

    其中的字段 ID 是跟另一个表有关联的
    select min(id)id 不太明白这句的意思,为什么会用到min(id)呢 
      

  9.   

    8i好像不支持分析函数...
    你的结果里的id是取分组里较大的那个吧。看错了,应该是max(id)
    将何波的语句改写一下
    你试试
    select * 
    from( 
    select max(id),姓名,sum(奖金) 
    from table 
    group by 姓名 
    order by 3 desc) 
    where rownum=1
      

  10.   

    select  * from (
    select max(id),name,sum(salar)
    from temp    group by name order by sum(salar) desc )
     where rownum=1
      

  11.   


    8i好像不支持分析函数...
    还有其它办法吗
    我要的结果是能查询出谁的奖金总额之和是最高的,能在结果中显示出奖金总额之和最高者的ID号 姓名和奖金总额
      

  12.   

    这个语句不是已经做到了吗
    如果奖金最高的可能不止一个
    可以试试
    with t as(
      select max(id)id,姓名,sum(奖金)奖金 from table1
      group by 姓名)
    select * from t t1 where not exists(
      select 1 from t where 奖金>t1.奖金)
      

  13.   

    with temp as(
         select 1 id, '张三' name,20 bonus from dual
         union all
         select 2 id, '李四' name,10 bonus from dual
         union all
         select 3 id, '王一' name,5 bonus from dual
         union all
         select 4 id, '王一' name,50 bonus from dual
         union all
         select 5 id, '李四' name,45 bonus from dual     
    )
    select * from (
    select max(id) id,name,sum(bonus) maxbonus from temp  group by name
    ) where not exists(
    select 1 from temp where maxbonus < temp.bonus
    )
      

  14.   

    SELECT tt.* FROM (SELECT t.t_id,t.t_name,t.t_comm FROM test1  t ORDER BY t.t_comm  DESC)tt
    WHERE ROWNUM=1
    ;
      

  15.   

    SELECT A.ID, A.NAME, A.T, A.AA  FROM (SELECT T.ID, T.NAME, T.T, SUM(T.T) OVER(PARTITION BY T.NAME) AS AA          FROM TEST111 T         ORDER BY AA DESC                ) AWHERE ROWNUM = 1