行转列,列转行,图一转图二或图二转图一

Posted suheng01

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了行转列,列转行,图一转图二或图二转图一相关的知识,希望对你有一定的参考价值。

图一:

Nam Course Score
zhangsan Chinese 85
zhangsan Maths 76
zhangsan English 80
lisi Chinese 82
lisi Maths 90
lisi English 55

图二:

Nam Chinese Maths English
zhangsan 85 76 80
lisi 82 90 55

图一转图二:

SELECT
    Nam,
    SUM(
        IF (Course = Chinese, Score, 0)
    ) Chinese,
    SUM(IF(Course = Maths, Score, 0)) Maths,
    SUM(
        IF (Course = English, Score, 0)
    ) english
FROM
    t
GROUP BY Nam

图二转图一:

(SELECT Nam, ChineseCourse, Chinese Score FROM t2)
UNION
(SELECT Nam, Maths Course , Maths Score FROM t2)
UNION
(SELECT Nam, English Course , English Score FROM t2)
ORDER BY Nam DESC

 

以上是关于行转列,列转行,图一转图二或图二转图一的主要内容,如果未能解决你的问题,请参考以下文章

oracle 多列 列转行

MySQL行转列与列转行

Hive 行转列 & 列转行

SAS行转列&&列转行

Oracle列转行,行转列

oracle行转列,列转行