zoukankan      html  css  js  c++  java
  • oracle行转列

    今天碰到一个行转列的问题,但是在网上看到很多的是列转行的,最后终于找到一个行转列的,在这里写一下,一来了方便自己看,二来了也方便有需要的朋友了参考下,我这里只是举个列子,具体还没深入研究。

    主要是两种方法:

    第一种:

    这是原数据,

    SQL> select * from s_student_result;

    NAME                        ENGLISH       MATH    PHYSISC    CHINESE TESTDATE
    ------------------------ ---------- ---------- ---------- ---------- -------------
    张三                             70         80         90         90 12-12月-11
    李四                             80         70         80         90 12-12月-11
    王五                             60         80         80         85 01-1月 -12

    需要转换为:


    NAME                     COURCE              SCORE TESTDATE
    ------------------------ -------------- ---------- --------------
    张三                     chinese                90 12-12月-11
    张三                     english                70 12-12月-11
    张三                     math                   80 12-12月-11
    张三                     pjysisc                90 12-12月-11
    李四                     chinese                90 12-12月-11
    李四                     english                80 12-12月-11
    李四                     math                   70 12-12月-11
    李四                     pjysisc                80 12-12月-11
    王五                     chinese                85 01-1月 -12
    王五                     english                60 01-1月 -12
    王五                     math                   80 01-1月 -12

    NAME                     COURCE              SCORE TESTDATE
    ------------------------ -------------- ---------- --------------
    王五                     pjysisc                80 01-1月 -12

    那么利用oracle中的union,sql语句如下:

    select  name,'english' cource,english as score ,testdate from s_student_result

    union  select  name,'math' cource,math as score, testdate from s_student_result t

    union  select  name,'pjysisc' cource,physisc as score ,testdate from s_student_result

    union  select  name,'chinese' cource,chinese as score, testdate from s_student_result

    第二种:

    创建一个中间表

    create table TEST_LH

    (

      NAME     VARCHAR2(20),

      COURCE   VARCHAR2(20),

      SCORE    NUMBER,

      TESTDATE DATE

    )

    然后插入语句

    insert all

     into test_lh values(name,'english',english,testdate)

     into test_lh values(name,'math',math,testdate)

     into test_lh values(name,'physisc',physisc,testdate)

     into test_lh values(name,'chinese',chinese,testdate)

    select name,t.english,t.math,t.physisc,t.chinese ,t.testdate from s_student_result t

    commit;

    然后查询结果如下:


    NAME                                     COURCE                                        SCORE TESTDATE
    ---------------------------------------- ---------------------------------------- ---------- -------
    张三                                     english                                          70 12-12月-11
    李四                                     english                                          80 12-12月-11
    王五                                     english                                          60 01-1月 -12
    张三                                     math                                             80 12-12月-11
    李四                                     math                                             70 12-12月-11
    王五                                     math                                             80 01-1月 -12
    张三                                     physisc                                          90 12-12月-11
    李四                                     physisc                                          80 12-12月-11
    王五                                     physisc                                          80 01-1月 -12
    张三                                     chinese                                          90 12-12月-11
    李四                                     chinese                                          90 12-12月-11

    NAME                                     COURCE                                        SCORE TESTDATE
    ---------------------------------------- ---------------------------------------- ---------- -------
    王五                                     chinese                                          85 01-1月 -12s

    列转行是相反的,这里就只写下sql语句(两种),

    select t.name,

    sum(decode(t.cource,'english',t.score)) as english,

    sum(decode(t.cource,'math',t.score)) as math,

    sum(decode(t.cource,'physisc',t.score)) as physisc,

    sum(decode(t.cource,'chinese',t.score))as chinese,

    t.testdate

    from test_lh t

    group by t.name,t.testdate

    select t.name,

     max(case cource when 'english' then score else 0 end) english,

     max(case cource when 'math' then score else 0 end) math,

     max(case cource when 'physisc' then score else 0 end) physisc,

     max(case cource when 'chinese' then score else 0 end) chinese,

     t.testdate

    from test_lh t

    group by t.name,t.testdate

  • 相关阅读:
    javascript 变量定义
    javascript之String
    javascript之object
    javascript之Number
    javascript之window对象
    javascript全局对象
    【NOIP2017】【Luogu3951】小凯的疑惑
    【NOIP2008】【Luogu1149】火柴棒等式
    【NOIP2008】【Luogu1125】笨小猴
    【NOIP2005】【Luogu1051】谁拿了最多奖学金
  • 原文地址:https://www.cnblogs.com/jinqi79731/p/2636585.html
Copyright © 2011-2022 走看看