case语句语法:
--简单Case函数
CASE sex
WHEN '1' THEN '男'
WHEN '2' THEN '女'
ELSE '其他' END
--Case搜索函数
CASE WHEN sex = '1' THEN '男'
WHEN sex = '2' THEN '女'
ELSE '其他' END

首先创建一张users表,其中包含id,name,sex三个字段,表内容如下:

复制代码
SQL> drop table users purge; drop table users purge ORA-00942: 表或视图不存在
SQL> create table users(id int,name varchar2(20),sex number); Table created
SQL> insert into users(id,name) values(1,'张一'); 1 row inserted
SQL> insert into users(id,name,sex) values(2,'张二',1); 1 row inserted
SQL> insert into users(id,name) values(3,'张三'); 1 row inserted
SQL> insert into users(id,name) values(4,'张四'); 1 row inserted
SQL> insert into users(id,name,sex) values(5,'张五',2); 1 row inserted
SQL> insert into users(id,name,sex) values(6,'张六',1); 1 row inserted
SQL> insert into users(id,name,sex) values(7,'张七',2); 1 row inserted
SQL> insert into users(id,name,sex) values(8,'张八',1); 1 row inserted
SQL> commit; Commit complete
SQL> select * from users; ID NAME SEX
--------------------------------------- -------------------- ----------
1 张一
2 张二 1
3 张三
4 张四
5 张五 2
6 张六 1
7 张七 2
8 张八 1 8 rows selected
复制代码
1、上表结果中的"sex"是用代码表示的,希望将代码用中文表示。可在语句中使用case语句: 复制代码
SQL> select u.id,u.name,u.sex,
2 (case u.sex
3 when 1 then '男'
4 when 2 then '女'
5 else '空的'
6 end
7 )性别
8 from users u; ID NAME SEX 性别
--------------------------------------- -------------------- ---------- ------
1 张一 空的
2 张二 1 男
3 张三 空的
4 张四 空的
5 张五 2 女
6 张六 1 男
7 张七 2 女
8 张八 1 男 8 rows selected
复制代码
2、如果不希望列表中出现"sex"列,语句如下: 复制代码
SQL> select u.id,u.name,
2 (case u.sex
3 when 1 then '男'
4 when 2 then '女'
5 else '空的'
6 end
7 )性别
8 from users u; ID NAME 性别
--------------------------------------- -------------------- ------
1 张一 空的
2 张二 男
3 张三 空的
4 张四 空的
5 张五 女
6 张六 男
7 张七 女
8 张八 男 8 rows selected
复制代码
3、将sum与case结合使用,可以实现分段统计。
如果现在希望将上表中各种性别的人数进行统计,sql语句如下: 复制代码
SQL> select
2 sum(case u.sex when 1 then 1 else 0 end)男性,
3 sum(case u.sex when 2 then 1 else 0 end)女性,
4 sum(case when u.sex <>1 and u.sex<>2 then 1 else 0 end)性别为空
5 from users u; 男性 女性 性别为空
---------- ---------- ----------
3 2 0 --------------------------------------------------------------------------------
SQL> select
2 count(case when u.sex=1 then 1 end)男性,
3 count(case when u.sex=2 then 1 end)女,
4 count(case when u.sex <>1 and u.sex<>2 then 1 end)性别为空
5 from users u; 男性 女 性别为空
---------- ---------- ----------
3 2 0

2.项目中实际应用

/**
* 方法名称: findAucAgencyDealCount<br>
* 描述: List 中的每一个对象都是一条记录<br>
* Object[]中下标值依次是对应的字段列<br>
* 执行原生的sql连接查询<br>
* @param startDate
* @param endDate
* @return 数组集合 即 List<Object[]>
*/
@Query(value = "SELECT s.name as aucagencyName,sum(a.qty_auction) as aucLotCount,"
+"sum(case when b.is_deal=1 and a.is_published=2 then b.qty_deal else 0 end) as aucLotDealCount,"
+"sum(case when b.is_deal=1 and b.is_settled=1 and a.is_published=2 then b.qty_deal else 0 end) as factCount,"
+"sum(case when b.is_deal=4 and a.is_published=2 then b.qty_deal else 0 end) as regretCount,"
+"sum(case when a.is_published=2 and a.type=1 then 1 else 0 end) as shootnumber,"
+"sum(case when a.is_published=2 and a.type=2 then 1 else 0 end) as sellnumber,"
+"sum(case when a.is_published=2 and b.is_deal=1 and a.type=1 then 1 else 0 end) as aucLotDealnumber,"
+"sum(case when a.is_published=2 and b.is_deal=1 and a.type=1 and b.is_priority=1 then 1 else 0 end) as firstnumber,"
+"sum(case when a.is_published=2 and a.type=2 and b.is_deal=1 then 1 else 0 end) as sellDealnumber "
+"from auc_lot a left join auc_brand b on a.id=b.auc_id left join sys_agency s on a.agency_id=s.id "
+"where (DATE_FORMAT(a.published_time,'%Y-%m-%d') BETWEEN :startDate and :endDate) "
+ "and a.agency_id in (:agencyIds)"
+"GROUP BY s.name",nativeQuery=true)
List<Object> findAucAgencyDealCount(@Param("startDate") String startDate, @Param("endDate") String endDate,@Param("agencyIds") List<Long> agencyIds);

sql 语句之 case的更多相关文章

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

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

  2. SQL语句中case函数

    case函数,严格的意义上来讲case函数已经试流程控制语句了,不是简单意义上的函数,不过为了方便,很多人将case函数称为流程控制函数. case函数的一般有两种用法:1.case expressi ...

  3. sql语句判断 case when用法

    sql语句判断方法之一 selectcase when t.bk1='on' then 1else 0 end  as 基础 ,case when t.bk2='on' then 1else 0 en ...

  4. Oracle的sql语句中case关键字的用法 & 单双引号的使用

    关于sql中单引号和双引号的使用,来一点说明: 1. 查询列的别名如果含有汉字或者特殊字符(如以'_'开头),需要用双引号引起来.而且只能用双引号,单引号是不可以的. 2. 如果想让某列返回固定的值, ...

  5. sql语句:CASE WHEN END 的用法

    select b,c, CASE a ' ' ' end from test1

  6. 条件分支SQL语句<一> Case When

    SELECT END ) AS MoneyIn, END ) AS MoneyOut, END ) AS BetMoney, END ) AS PctMoney, END ) AS WinMoney, ...

  7. SQL语句中case,when,then的用法

    用法如下bai: 复制代码 SELECT s.s_id, s.s_name, s.s_sex, CASE WHENs.s_sex='1'THEN'男' WHENs.s_sex='2'THEN'女' E ...

  8. SQL面试题: 数据库中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列 ,当B列大于C列时选择B列否则选择C列 ,

    1.用一条sql语句 select (case when a>b then a else b end ),(case when b>c then b esle c end)  from 表 ...

  9. 精妙的SQL语句

      说明:复制表(只复制结构,源表名:a 新表名:b)select * into b from a where 1<>1 说明:拷贝表(拷贝数据,源表名:a 目标表名:b)insert i ...

随机推荐

  1. bzoj 1415 期望dp + 记忆化搜索

    思路:这个题看着感觉不能dp,其实是可以dp的,因为狼每次走两步,兔子每次走一步,每进行一轮以后,狼和兔子的距离 肯定是在接近的,没有相同的状态,dp之前预处理出来,每一步狼该往哪里走. #inclu ...

  2. 洛谷P2464 [SDOI2008] 郁闷的小j [分块]

    题目传送门 郁闷的小j 题目描述 小J是国家图书馆的一位图书管理员,他的工作是管理一个巨大的书架.虽然他很能吃苦耐劳,但是由于这个书架十分巨大,所以他的工作效率总是很低,以致他面临着被解雇的危险,这也 ...

  3. Python 函数系列 - Str对path的处理

    由此可见,“\”是转义字符,它能够将第2个“\”从转义字符转回普通字符,从而“\n”就不再起到换行符的作用. 这样操作虽然简单,但是遇到下方这个路径,看起来就会有些麻烦! path = 'D:\new ...

  4. AM335x开发板与PC机虚拟机建立tftp文件传输

    1.AM335x开发板必须要支持以太网,而且在U-boot中要有完好的以太网驱动 因为开发板的储存介质为SD卡,所以在编译好的U-boot中并没有配置网络环境,为了不使每次上电都修改u-boot的网络 ...

  5. 大数据开篇 MapReduce初步

    最近在学习大数据相关的东西,开这篇专题来记录一下学习过程.今天主要记录一下MapReduce执行流程解析 引子(我们需要解决一个简单的单词计数(WordCount)问题) 1000个单词 嘿嘿,100 ...

  6. poj3259(spfa)

    自己的第一道spfa,纪念一下,顺便转载一下spfa的原理.先po代码: #include <iostream> #include <queue> using namespac ...

  7. Hat's Fibonacci hdu 1250

    Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...

  8. 性能测试工具——Jmeter使用小结(一)

    Apache Jmeter是针对Java的一款性能测试工具,利用该工具可以实现自动化的批量测试和结果聚合,适合做接口压测.今天就来捋一捋软件安装的一些小细节和使用. 一.安装 Jmeter基于JDK, ...

  9. bzoj 2809: [Apio2012]dispatching -- 可并堆

    2809: [Apio2012]dispatching Time Limit: 10 Sec  Memory Limit: 128 MB Description 在一个忍者的帮派里,一些忍者们被选中派 ...

  10. bzoj 1231: [Usaco2008 Nov]mixup2 混乱的奶牛 -- 状压DP

    1231: [Usaco2008 Nov]mixup2 混乱的奶牛 Time Limit: 10 Sec  Memory Limit: 162 MB Description 混乱的奶牛 [Don Pi ...