表结构学号   分数   科目
1      90     数学
1      91     英语
1      92     语文
1      93     生物
2      90     数学
2      91     英语
2      92     语文
3      91     英语
3      92     语文
3      93     生物求每个学号的总分,最高分,以及最高分对应的科目。怎么写sql,不要用with

解决方案 »

  1.   

    select m.* , n.科目 from
    (select 学号 , sum(分数) "总分" , max(分数) "最高分" from tb group by 学号),
    (select t.* from tb t where 分数 = (select max(分数) from tb where 学号 = t.学号)) n
    where m.学号 = n.学号
    order by m.学号select m.* , n.科目 from
    (select 学号 , sum(分数) "总分" , max(分数) "最高分" from tb group by 学号),
    (select t.* from tb t where not exists (select 1 from tb where 学号 = t.学号 and 分数 > t.分数)) n
    where m.学号 = n.学号
    order by m.学号
      

  2.   

    select sumscore.sno , sumscore.sumsc , 
    highest.score as highscore, highest.sub 
    from
    (select sno, sum(score) sumsc from test_yixl group by sno) sumscore,
    (select sno, score, sub from (select sno, score, sub, row_number() over (partition by sno order by score desc) cnt
    from test_yixl) where cnt = 1) highest
    where sumscore.sno = highest.sno;
      

  3.   

    --创建学生成绩表
    CREATE TABLE JBGSW.STUDENTSCORE
    (
      XUEHAO  NUMBER,
      FENSHU  NUMBER,
      KEMU    VARCHAR2(50 BYTE)
    )
    --插入测试数据--SQL code
    select c.xuehao as 学号,zfenshu as 总分,fenshu as 最高分,kemu as 科目 from(
    select xuehao,sum(fenshu) as zfenshu from STUDENTSCORE
    group by xuehao
    )c,
    (
    select a.xuehao,kemu,fenshu from STUDENTSCORE a ,(select xuehao,max(fenshu) as gfenshu from STUDENTSCORE group by xuehao)b where a.xuehao=b.xuehao and a.fenshu=b.gfenshu
    )d where c.xuehao=d.xuehao
      

  4.   

    select A.studentNo,A.总分,A.最高分,B.科目 
    from (select 学号 AS studentNo,MAX(分数) AS 最高分,sum(分数) AS 总分 from 表A
    group by studentNo) B,表A 
    where B.studentNo=A.学号 and A.分数=B.最高分;
      

  5.   

    可以这样实现SELECT b.*,a.kemu FROM a a,
    (SELECT id,SUM(score) total,MAX(score) mx FROM a GROUP BY ID) b
    WHERE a.Id=b.ID AND a.score=b.mx;
      

  6.   


    ---
    select 学号,max(分数),max(科目)keep(dense_rank first order by 分数 desc)
      from t1
     group by 学号;--
    select 学号,分数,科目
      from (select 学号,分数,科目,case when max(分数)over(partition by 学号)=分数 then 1 else 0 end flag
             from t1)
     where flag = 1
      

  7.   

    select a.sno,a.sum_score,a.max_score,t.subject from tbsql t,
    (
    select sno,sum(score)  sum_score,max(score) max_score from  tbsql group by sno
    ) a
    where T.SNO=a.sno and T.SCORE=a.max_score
    测试成功!
      

  8.   

    select s.* from table s inner join
    (
    select m.id sum(m.sore),avg(s.sore) from table m
    group by m.id
    ) on s.id=m.id
      

  9.   

    分开求总分和最高分,写Group by语句, 然后再用连接就行了
      

  10.   


    create table T005
    (
        stu_no  varchar2(20),
        score   varchar2(20),
        subjects  varchar2(20)
    );insert into T005 values('1','90','数学');
    insert into T005 values('1','91','英语');
    insert into T005 values('1','92','语文');
    insert into T005 values('1','93','生物');
    insert into T005 values('2','90','数学');
    insert into T005 values('2','91','英语');
    insert into T005 values('2','92','语文');
    insert into T005 values('3','91','英语');
    insert into T005 values('3','92','语文');
    insert into T005 values('3','93','生物');
    select t1.stu_no,
    t2.sum_score,
    t2.top_score,
    t1.subjects 
    from t005 t1,(select stu_no,
                  sum(score) as sum_score,
                  max(score)  as top_score
            from  t005
            group by stu_no) t2
    where t1.stu_no = t2.stu_no
    and   t1.score = t2.top_score
    order by stu_no;STU_NO SUM_SCORE TOP_SCORE SUBJECTS
    1 366 93 生物
    2 273 92 语文
    3 276 93 生物
      

  11.   

    select sum(分数),max(科目) keep(dense_rank first order by 分数 desc) first,max(分数) keep(dense_rank first order by 分数 desc) first from 表名 group by 学号