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

    2.向TEST表中添加数据

    INSERT INTO TEST(STUDENT,COURSE,SCORE)
    select '张三','语文',78 from dual union
    select '张三','数学',87 from dual union 
    select '张三','英语',82 from dual union
    select '张三','物理',90 from dual union
    select '李四','语文',65 from dual union
    select '李四','数学',77 from dual union
    select '李四','英语',65 from dual union
    select '李四','物理',85 from dual

    表数据如下:

    3.列转行

    方法··1:

    select 
        Student,
        sum(decode(Course, '数学', Score)) 数学,
        sum(decode(Course, '物理', Score)) 物理,
        sum(decode(Course, '英语', Score)) 英语,
        sum(decode(Course, '语文', Score)) 语文
    from 
        TEST 
    group by Student

    方法··2:

    select
        Student,
        sum(case Course when '数学' then Score else null end) 数学,
        sum(case Course when '物理' then Score else null end) 物理,
        sum(case Course when '英语' then Score else null end) 英语,
        sum(case Course when '语文' then Score else null end) 语文
    from 
        TEST 
    group by Student

    效果如下:

  • 相关阅读:
    【SCOI 2011】 糖果
    【POJ 3159】 Candies
    【POJ 1716】 Integer Intervals
    【POJ 2983】 Is the information reliable?
    【POJ 1364】 King
    【POJ 1201】 Intervals
    【POJ 1804】 Brainman
    6月10日省中提高组题解
    【POJ 3352】 Road Construction
    【POJ 1144】 Network
  • 原文地址:https://www.cnblogs.com/joyous-day/p/6951565.html
Copyright © 2011-2022 走看看