zoukankan      html  css  js  c++  java
  • mysql行转列 问题 SUM(IF(条件,列值,0))

    sum(if(条件,列值,0))语法用例:

    select name,sum(if(subject="语文",score,0)) as “语文” from grade GROUP BY name

    if中第一个参数为条件,符合条件的内容才会将第二个参数相加,不符合的内容将第三个参数相加。

    第二个参数为要相加的参数,可以用常量,也可以用字段值。

    第三个参数为条件为假时相加的值,通常设为0.

    为了将行转为列(这种操作一般用于统计),可使用如下语句:

     select name,sum(if(subject="语文",score,0)) as “语文”,

    sum(if(subject="数学",score,0)) as “数学”,sum(if(subject="英语",score,0)) as “英语”

    from grade GROUP BY name

    结果

    mysql行列转换

     

     

    创建数据库、表

    复制代码
    create database tests;
    use tests;
    create table t_score(
    id int primary key auto_increment,
    name varchar(20) not null,  #名字
    Subject varchar(10) not null, #科目
    Fraction double default 0  #分数
    );
    复制代码

    添加数据

    复制代码
    INSERT INTO `t_score`(name,Subject,Fraction) VALUES
             ('王海', '语文', 86),
            ('王海', '数学', 83),
            ('王海', '英语', 93),
            ('陶俊', '语文', 88),
            ('陶俊', '数学', 84),
            ('陶俊', '英语', 94),
            ('刘可', '语文', 80),
            ('刘可', '数学', 86),
            ('刘可', '英语', 88),
            ('李春', '语文', 89),
            ('李春', '数学', 80),
            ('李春', '英语', 87);
    复制代码

     方式一:使用if

    复制代码
    select name as 名字 ,
    sum(if(Subject='语文',Fraction,0)) as 语文,
    sum(if(Subject='数学',Fraction,0))as 数学, 
    sum(if(Subject='英语',Fraction,0))as 英语,
    round(AVG(Fraction),2) as 平均分,
    SUM(Fraction) as 总分
    from t_score group by name     
    union
    select name as 名字 , sum(语文) Chinese,sum(数学) Math,sum(英语) English,round(AVG(总分),2)as 平均分,sum(总分) score  from(
    select 'TOTAL' as name,
    sum(if(Subject='语文',Fraction,0)) as 语文,
    sum(if(Subject='数学',Fraction,0))as 数学, 
    sum(if(Subject='英语',Fraction,0))as 英语,
    SUM(Fraction) as 总分
    from t_score group by Subject )t
    复制代码

    方式二:使用case

    复制代码
    select  name as Name,
    sum(case when Subject = '语文' then Fraction end) as Chinese,
    sum(case when Subject = '数学' then Fraction end) as Math,
    sum(case when Subject = '英语' then Fraction end) as English,
    sum(fraction)as score
    from t_score group by name
    UNION ALL
    select  name as Name,sum(Chinese) as Chinese,sum(Math) as Math,sum(English) as English,sum(score) as score from(
    select 'TOTAL' as name,
    sum(case when Subject = '语文' then Fraction end) as Chinese,
    sum(case when Subject = '数学' then Fraction end) as Math,
    sum(case when Subject = '英语' then Fraction end) as English,
    sum(fraction)as score
    from t_score group by Subject)t
    复制代码

    方法三: with rollup

    复制代码
     select 
            ifnull(name,'TOll') name,
            sum(if(Subject='语文',Fraction,0)) as 语文,
           sum(if(Subject='英语',Fraction,0)) as 英语,
           sum(if(Subject='数学',Fraction,0))as 数学,
           sum(Fraction) 总分
            from t_score group by name with rollup
    复制代码

     

     
  • 相关阅读:
    飘逸的python
    hdu 4512 吉哥系列故事——完美队形I(最长公共上升自序加强版)
    Codeforces 482B. Interesting Array 线段树
    《开源框架那些事儿21》:巧借力与借巧力
    openNebula libvirt-virsh attach disk device for kvm
    configure mount nfs
    opennebula kvm attach disk
    openNebula 运维系列虚拟机virtual machines operations
    yum subversion puppet puppet-server
    文件系统,快存储,对象存储
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14008427.html
Copyright © 2011-2022 走看看