有以下表:CREATE TABLE test(name  VARCHAR2(10)  NOT NULL,
  km  VARCHAR2(10)  NOT NULL,
  cj    NUMBER(6)   NOT NULL 
 )  
    insert into  test values('张三','语文',80);
insert  into test values('张三','数学',86);
insert into test values('张三','英语',75);
insert   into test values('李四','语文',78);
insert into test values('李四','数学',85);
insert  into test values('李四','英语',78);问题是:如何写一过程,使结果在最后增加一行总分,来统计每个科目的总分??(注意:1.不是每个人的总分,而是每个科目的总分 2.科目是动态的,即不一定几科)    要求的结果如下:姓名   语文   数学   英语 ....
 张三   80     86     75
 李四   78     85     78
.....
 总分  xxx    xxx    xxx

解决方案 »

  1.   

    select name, max(decode(km, '语文',  cj, null)) "语文",
          max(decode(km, '数学',  cj, null)) "数学",
          max(decode(km, '英语',  cj, null)) "英语"
    from test
    union
    select '总分', sum(语文), sum(数学), sum(英语)
    from (
    select name, max(decode(km, '语文',  cj, null)) "语文",
          max(decode(km, '数学',  cj, null)) "数学",
          max(decode(km, '英语',  cj, null)) "英语"
    )
      

  2.   

    select name, max(decode(km, '语文', cj, null)) "语文",
    max(decode(km, '数学', cj, null)) "数学",
    max(decode(km, '英语', cj, null)) "英语"
    from test
    group by name
    union
    select '总分', sum(语文), sum(数学), sum(英语)
    from (
    select name, max(decode(km, '语文', cj, null)) "语文",
    max(decode(km, '数学', cj, null)) "数学",
    max(decode(km, '英语', cj, null)) "英语"
    from test
    group by name
    )
      

  3.   

    但科目是动态的,即不一定几科,能否用个句子代替:
        max(decode(km, '数学', cj, null)) "数学",
    max(decode(km, '英语', cj, null)) "英语"
        不需把具体科目列出