zoukankan      html  css  js  c++  java
  • Sql语句-case when then else end

    依据上面的表信息输出以下的结果:



    以下是建库和表结构据:

    create table DeptSales
    (
    	deptID int,
    	SubjMonth int ,
    	sales int ,
    	deptname varchar(50)
    )
    

    insert into deptsales (deptid ,subjmonth,sales) values (1,1,55);
    insert into deptsales (deptid ,subjmonth,sales) values (2,1,66);
    insert into deptsales (deptid ,subjmonth,sales) values (3,1,77);
    insert into deptsales (deptid ,subjmonth,sales) values (2,2,34);
    insert into deptsales (deptid ,subjmonth,sales) values (4,2,56);
    insert into deptsales (deptid ,subjmonth,sales) values (3,3,78);
    

    运行sql语句:

    mysql> select deptID as '部门',sum(case SubjMonth when 1 then sales end) '一月销售额',sum(case SubjMonth when 2 then sales end) '二月销售额',sum(case SubjMonth when 3 then sales end) '三月销售额',sum(case SubjMonth when 4 then sales end) '四月销售额'  from deptsales d group by deptID;
    +------+------------+------------+------------+------------+
    | 部门 | 一月销售额 | 二月销售额 | 三月销售额 | 四月销售额 |
    +------+------------+------------+------------+------------+
    |    1 | 55         | NULL       | NULL       | NULL       |
    |    2 | 66         | 34         | NULL       | NULL       |
    |    3 | 77         | NULL       | 78         | NULL       |
    |    4 | NULL       | 56         | NULL       | NULL       |
    +------+------------+------------+------------+------------+
    4 rows in set


  • 相关阅读:
    SpringBoot打包 jar文件太小 无jar
    公共dns
    SpringBoot解决跨域问题
    git学习-来自慕课网
    SpringBoot Value 'xxx' is not a valid duration
    Maven生成项目
    github-自我使用-滑稽
    jsonp示列
    mysqldump 备份和恢复整理
    myql二进制日志
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/3997825.html
Copyright © 2011-2022 走看看