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

以下是建库和表结构据:

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

Sql语句-case when then else end的更多相关文章

  1. SQL语句case when then的用法

    case具有两种格式.简单case函数和case搜索函数. //简单case函数 case sex when '1' then '男' when '2' then '女’ else '其他' end ...

  2. sql语句 case when then else end 语句实例

    表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列. ----------------------------------------- ...

  3. sql语句-CASE WHEN用法详解

    当我们需要从数据源上 直接判断数据显示代表的含义的时候 ,就可以在SQL语句中使用 Case When这个函数了. Case具有两种格式.简单Case函数和Case搜索函数. 第一种 格式 : 简单C ...

  4. sql语句case when 以及left()

    select count(CASE jyje WHEN '1300' THEN '2' ELSE '1' END) as count  from tpent_orders where cplx = 6 ...

  5. sql语句 case

    case: SELECT stdname, max( CASE WHEN stdsubject = '语文' THEN result ELSE 0 END) "语文", max( ...

  6. SQL 语句case when

    简介 case when 一般有两种书写方式,多用于查询判断 1. case 列名 when '' then '空' ' then '成功' ' then '失败' else '其他' end as ...

  7. SQL语句 case ... when

    select 字段A, 字段B, CASE WHEN t.operate = 1 then 'aa'WHEN t.operate = 2 then 'bb'WHEN t.operate = 3 the ...

  8. 记录一个SQL语句 case select 1

    select Code, CodeName, CodeAlias, ComCode, OtherSign from ldcode where codetype = 'edorapptype' and ...

  9. 如何用ORM支持SQL语句的CASE WHEN?

    OQL如何支持CASE WHEN? 今天,一个朋友问我,OQL可否支持CASE WHEN语句?他给的示例SQL如下: then '启用' else '停用' from tb_User OQL是SOD框 ...

随机推荐

  1. 解决uc浏览器不支持vw单位的方法

    插入下段代码,使用rem来代替vw <script type="text/javascript"> (function (doc, win) { var docEl = ...

  2. java IntelliJ IDEA 13 注册码 IDEA序列号 License Key

    java IntelliJ IDEA 13 注册码 IDEA序列号 License Key Username: JavaDeveloper@sskaje.me License: 282971-M1NW ...

  3. Tutorial: Reverse debugging with GDB 7 (转载)

    Tutorial: Reverse debugging with GDB 7 Tutorial: Reverse debugging with GDB 7 by Jay Conrod posted o ...

  4. 一道关于数据库(经典父子级 ID 关联)更新题,大家帮忙想想还有其它解决思路没有?

    昨天,一同事发过来的一道数据库题目,就是哪种经典的父子级 ID 在同一数据库表中设计类型.需要在原表中添加一个字段,同时,将该节点的父子级详细信息插入到原表新增的一字段中,具体效果如下图. AreaC ...

  5. explode() 字符串转换数组

    explode() 函数把字符串分割为数组. 语法 explode(separator,string,limit) 例子: $str = "Hello world. It's a beaut ...

  6. thinkphp5.0模块设计

    5.0版本对模块的功能做了灵活设计,默认采用多模块的架构,并且支持单一模块设计,所有模块的命名空间均以app作为根命名空间(可配置更改). 目录结构 标准的应用和模块目录结构如下: ├─applica ...

  7. 不通过注册表使用ActiveX对象

    为了弄清楚COM库的运行原理,特意做了这个实验: #include "stdafx.h" #include "objbase.h" #include " ...

  8. 转:linux关闭防火墙iptables

    ref:https://jingyan.baidu.com/article/066074d64f433ec3c21cb000.html Linux系统下面自带了防火墙iptables,iptables ...

  9. python统计文本中每个单词出现的次数

    .python统计文本中每个单词出现的次数: #coding=utf-8 __author__ = 'zcg' import collections import os with open('abc. ...

  10. 执行Shell脚本的4种方法及区别介绍(转)

    原文地址: http://www.jb51.net/article/66824.htm 执行shell脚本有以下几种方式 ###1.相对路径方式,需先cd到脚本路径下 [root@banking tm ...