MySQL如何实现行转列分级输出?

转:http://www.2cto.com/database/201511/450113.html

概述

好久没写SQL语句,今天看到问答中的一个问题,拿来研究一下。

问题链接:关于Mysql 的分级输出问题

情景简介

学校里面记录成绩,每个人的选课不一样,而且以后会添加课程,所以不需要把所有课程当作列。数据表里面数据如下图,使用姓名+课程作为联合主键(有些需求可能不需要联合主键)。本文以MySQL为基础,其他数据库会有些许语法不同。

数据库表数据:

\\

处理后的结果(行转列):

\\

方法一:

这里可以使用Max,也可以使用Sum;

注意第二张图,当有学生的某科成绩缺失的时候,输出结果为Null; 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
SELECT
    SNAME,
    MAX(
        CASECNAME
        WHEN'JAVA'THEN
            SCORE
        END
    ) JAVA,
    MAX(
        CASECNAME
        WHEN'mysql'THEN
            SCORE
        END
    ) mysql
FROM
    stdscore
GROUPBY
    SNAME;

 

可以在第一个Case中加入Else语句解决这个问题: 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
SELECT
    SNAME,
    MAX(
        CASECNAME
        WHEN'JAVA'THEN
            SCORE
        ELSE
            0
        END
    ) JAVA,
    MAX(
        CASECNAME
        WHEN'mysql'THEN
            SCORE
        ELSE
            0
        END
    ) mysql
FROM
    stdscore
GROUPBY
    SNAME;

方法二: 

?
1
2
3
4
SELECTDISTINCT a.sname,
(SELECTscoreFROMstdscore bWHEREa.sname=b.snameANDb.CNAME='JAVA')AS'JAVA',
(SELECTscoreFROMstdscore bWHEREa.sname=b.snameANDb.CNAME='mysql')AS'mysql'
FROMstdscore a

方法三:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
DROPPROCEDURE
IF EXISTS sp_score;
DELIMITER &&
 
CREATEPROCEDUREsp_score ()
BEGIN
    #课程名称
    DECLARE
        cname_nVARCHAR(20) ; #所有课程数量
        DECLARE
            countINT; #计数器
            DECLARE
                iINTDEFAULT0 ; #拼接SQL字符串
            SET@s ='SELECT sname';
            SETcount= (
                SELECT
                    COUNT(DISTINCTcname)
                FROM
                    stdscore
            ) ;
            WHILE i <countDO
 
 
            SETcname_n = (
                SELECT
                    cname
                FROM
                    stdscore
                GROUPBYCNAME
                LIMIT i,
                1
            ) ;
            SET@s = CONCAT(
                @s,
                ', SUM(CASE cname WHEN ',
                '\'',
                cname_n,
                '\'',
                ' THEN score ELSE 0 END)',
                ' AS ',
                '\'',
                cname_n,
                '\''
            ) ;
            SETi = i + 1 ;
            END
            WHILE ;
            SET@s = CONCAT(
                @s,
                ' FROM stdscore GROUP BY sname'
            ) ; #用于调试
            #SELECT@s;
            PREPAREstmt
            FROM
                @s ;EXECUTEstmt ;
            END&&
 
CALL sp_score () ;

处理后的结果(行转列)分级输出:

\\ 

方法一: 

这里可以使用Max,也可以使用Sum;

注意第二张图,当有学生的某科成绩缺失的时候,输出结果为Null; 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
SELECT
    SNAME,
    MAX(
        CASECNAME
        WHEN'JAVA'THEN
            (
                CASE
                WHENSCORE - (selectavg(SCORE)fromstdscorewhereCNAME='JAVA') > 20THEN
                    '优秀'
                WHENSCORE - (selectavg(SCORE)fromstdscorewhereCNAME='JAVA') > 10THEN
                    '良好'
                WHENSCORE - (selectavg(SCORE)fromstdscorewhereCNAME='JAVA') >= 0THEN
                    '普通'
                ELSE
                    '较差'
                END
            )
        END
    ) JAVA,
    MAX(
        CASECNAME
        WHEN'mysql'THEN
            (
                CASE
                WHENSCORE - (selectavg(SCORE)fromstdscorewhereCNAME='JAVA') > 20THEN
                    '优秀'
                WHENSCORE - (selectavg(SCORE)fromstdscorewhereCNAME='JAVA') > 10THEN
                    '良好'
                WHENSCORE - (selectavg(SCORE)fromstdscorewhereCNAME='JAVA') >= 0THEN
                    '普通'
                ELSE
                    '较差'
                END
            )
        END
    ) mysql
FROM
    stdscore
GROUPBY
    SNAME;

方法二:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
SELECTDISTINCT a.sname,
(SELECT(
                CASE
                WHENSCORE - (selectavg(SCORE)fromstdscorewhereCNAME='JAVA') > 20THEN
                    '优秀'
                WHENSCORE - (selectavg(SCORE)fromstdscorewhereCNAME='JAVA') > 10THEN
                    '良好'
                WHENSCORE - (selectavg(SCORE)fromstdscorewhereCNAME='JAVA') >= 0THEN
                    '普通'
                ELSE
                    '较差'
                END
            )FROMstdscore bWHEREa.sname=b.snameANDb.CNAME='JAVA')AS'JAVA',
(SELECT(
                CASE
                WHENSCORE - (selectavg(SCORE)fromstdscorewhereCNAME='JAVA') > 20THEN
                    '优秀'
                WHENSCORE - (selectavg(SCORE)fromstdscorewhereCNAME='JAVA') > 10THEN
                    '良好'
                WHENSCORE - (selectavg(SCORE)fromstdscorewhereCNAME='JAVA') >= 0THEN
                    '普通'
                ELSE
                    '较差'
                END
            )FROMstdscore bWHEREa.sname=b.snameANDb.CNAME='mysql')AS'mysql'
FROMstdscore a

方法三:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
DROPPROCEDURE
IF EXISTS sp_score;
DELIMITER &&
 
CREATEPROCEDUREsp_score ()
BEGIN
    #课程名称
    DECLARE
        cname_nVARCHAR(20) ; #所有课程数量
        DECLARE
            countINT; #计数器
            DECLARE
                iINTDEFAULT0 ; #拼接SQL字符串
            SET@s ='SELECT sname';
            SETcount= (
                SELECT
                    COUNT(DISTINCTcname)
                FROM
                    stdscore
            ) ;
            WHILE i <countDO
 
 
            SETcname_n = (
                SELECT
                    cname
                FROM
                    stdscore
        GROUPBYCNAME
                LIMIT i, 1
            ) ;
            SET@s = CONCAT(
                @s,
                ', MAX(CASE cname WHEN ',
                '\'',
                cname_n,
                '\'',
                ' THEN (
                CASE
                WHEN SCORE - (select avg(SCORE) from stdscore where CNAME=\'',cname_n,'\') > 20 THEN
                    \'优秀\'
                WHEN SCORE - (select avg(SCORE) from stdscore where CNAME=\'',cname_n,'\') > 10 THEN
                    \'良好\'
                WHEN SCORE - (select avg(SCORE) from stdscore where CNAME=\'',cname_n,'\') >= 0 THEN
                    \'普通\'
                ELSE
                    \'较差\'
                END
            ) END)',
                ' AS ',
                '\'',
                cname_n,
                '\''
            ) ;
            SETi = i + 1 ;
            END
            WHILE ;
            SET@s = CONCAT(
                @s,
                ' FROM stdscore GROUP BY sname'
            ) ;
            #用于调试
            #SELECT@s;
            PREPAREstmt
            FROM
                @s ;EXECUTEstmt ;
            END&&
 
 
CALL sp_score ();

几种方法比较分析

第一种使用了分组,对每个课程分别处理。
第二种方法使用了表连接。
第三种使用了存储过程,实际上可以是第一种或第二种方法的动态化,先计算出所有课程的数量,然后对每个分组进行课程查询。这种方法的一个最大的好处是当新增了一门课程时,SQL语句不需要重写。

小结

关于行转列和列转行

这个概念似乎容易弄混,有人把行转列理解为列转行,有人把列转行理解为行转列;

这里做个定义:

行转列:把表中特定列(如本文中的:CNAME)的数据去重后做为列名(如查询结果行中的“JAVA,mysql”,处理后是做为列名输出);

列转行:可以说是行转列的反转,把表中特定列(如本文处理结果中的列名“JAVA,mysql”)做为每一行数据对应列“CNAME”的值; 

关于效率

不知道有什么好的生成模拟数据的方法或工具,麻烦小伙伴推荐一下,抽空我做一下对比; 

还有其它更好的方法吗?

本文使用的几种方法应该都有优化的空间,特别是使用存储过程的话会更加灵活,功能更强大;

本文的分级只是给出一种思路,分级的方法如果学生的成绩相差较小的话将失去意义;

如果小伙伴有更好的方法,还请不吝赐教,感激不尽! 

有些需求可能不需要联合主键

有些需求可能不需要联合主键,因为一门课程可能允许学生考多次,取最好的一次成绩,或者取多次的平均成绩。

    A+
发布日期:2015年11月27日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: