oracle数据库查询语句case的用法
实现功能:
1.先查询status=2的记录,如果查询到记录则返回第一条记录的Product_Name;
2.如果查询不到status=2的记录,则判断status=1的记录是否存在,不存在则返回“请耐心等待!”,存在则返回“拆烟完毕!”
实现思路:
1、使用case语句返回不同的内容:
case语句格式:
case when 判断语句1 then 返回1 when 判断语句2 then 返回2 …… else 返回n end
2、解决无记录时需要返回一条null的数据:
select Product_name from v_supply_sj union select null from dual
3、解决有记录的情况下还会附带一条null数据的问题:
通过判断初始查询结果count(*)是否等于0来决定返回的记录数
最终查询语句:
select case
when t1.Product_name is not null then t1.product_name
when t2.Product_name is not null then '拆烟完毕'
else '请耐心等待' end as ledtext from
(select rownum as q, Product_name from (select Product_name from( select rownum as p,Product_name from v_supply_sj where to_addr = 'SJ03' and bill_type=5 and status = 2 and up_down = '' ) where rownum<=1 union select null from dual )
where rownum<= case
when (select count(*) from (select Product_name from( select rownum as p,Product_name from v_supply_sj where to_addr = 'SJ03' and bill_type=5 and status = 2 and up_down = '' ) where rownum<=1 union select null from dual ))=1
then 1 else
(select count(*) from (select Product_name from( select rownum as p,Product_name from v_supply_sj where to_addr = 'SJ03' and bill_type=5 and status = 2 and up_down = '' ) where rownum<=1 ) ) end ) t1 left join
(select rownum as q, Product_name from (select Product_name from( select rownum as p,Product_name from v_supply_sj where to_addr = 'SJ03' and bill_type=5 and status = 1 and up_down = '' ) where rownum<=1 union select null from dual )
where rownum<= case
when (select count(*) from (select Product_name from( select rownum as p,Product_name from v_supply_sj where to_addr = 'SJ03' and bill_type=5 and status = 1 and up_down = '' ) where rownum<=1 union select null from dual ))=1
then 1 else
(select count(*) from (select Product_name from( select rownum as p,Product_name from v_supply_sj where to_addr = 'SJ03' and bill_type=5 and status = 1 and up_down = '' ) where rownum<=1 ) ) end ) t2 on t1.q=t2.q
oracle数据库查询语句case的用法的更多相关文章
- Oracle数据库查询语句
编写以下查询的SQL语句,以scott用户的emp表和dept表作为查询数据: 1.列出至少有一个员工的所有部门. SQL语句: select * from SCOTT.DEPT where dept ...
- oracle数据库查询日期sql语句(范例)、向已经建好的表格中添加一列属性并向该列添加数值、删除某一列的数据(一整列)
先列上我的数据库表格: c_date(Date格式) date_type(String格式) 2011-01-01 0 2012-03-07 ...
- Oracle数据库中序列(SEQUENCE)的用法详解
Oracle数据库中序列(SEQUENCE)的用法详解 在Oracle数据库中,序列的用途是生成表的主键值,可以在插入语句中引用,也可以通过查询检查当前值,或使序列增至下一个值.本文我们主要介绍了 ...
- Oracle分页查询语句的写法(转)
Oracle分页查询语句的写法(转) 分页查询是我们在使用数据库系统时经常要使用到的,下文对Oracle数据库系统中的分页查询语句作了详细的介绍,供您参考. Oracle分页查询语句使我们最常用的 ...
- 各种oracle参数查询语句
各种oracle参数查询语句 1.show parameter:--显示各个系统参数配置 2.select * from v$parameter;--显示各个系统参数配置 2.show paramet ...
- python操作oracle数据库-查询
python操作oracle数据库-查询 参照文档 http://www.oracle.com/technetwork/cn/articles/dsl/mastering-oracle-python- ...
- C#连接Oracle数据库查询数据
C#连接Oracle数据库可以实现许多我们需要的功能,下面介绍的是C#连接Oracle数据库查询数据的方法,如果您对C#连接Oracle数据库方面感兴趣的话,不妨一看. using System; u ...
- Oracle分页查询语句的写法
分页查询是我们在使用数据库系统时经常要使用到的,下文对Oracle数据库系统中的分页查询语句作了详细的介绍,供您参考. AD:2013云计算架构师峰会精彩课程曝光 Oracle分页查询语句使我们最常用 ...
- Oracle层级询语句connect by 用法详解
如果表中包含层级数据,那么你就可以使用层级查询从句选择行层级顺序. 1.层级查询从句语法 层级查询从句语法: { CONNECT BY [ NOCYCLE ] condition [AND condi ...
随机推荐
- 新ubuntu系统装软件
新装的ubuntu系统安装软件: 1.ifconfig #sudo apt-get install net-tools 2.vim #sudo apt-get install vim 3.telnet ...
- 学习python第三天
变量的命名规范 1.只能有 字母 数字 及_组成 2.不能以数字开头 3.避免与系统关键词重名:重名不会报错,但系统的功能就被自定义的功能屏蔽掉了(严重不建议这样做) 4._开头的变量都有特出含义 5 ...
- jumpservice一步一步安装
一步一步安装 (CentOS) 本文档旨在帮助用户了解各组件之间的关系, 生产环境部署建议参考 进阶安装文档 云服务器快速部署参考 极速安装 安装过程中遇到问题可参考 安装过程中常见的问题 测试推荐环 ...
- sqlserver 已星期一为第一天统计周
本文来源:https://blog.csdn.net/sqlserverdiscovery/article/details/53080695 SELECT GETDATE() AS THEDAY, c ...
- LeetCode 791. Custom Sort String
题目链接:https://leetcode.com/problems/custom-sort-string/description/ S and T are strings composed of l ...
- Layout-2相关代码:3列布局代码演化[一]
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 团队第十次 # scrum meeting
github 本此会议项目由PM召开,召开时间为4-15日晚上9点,以大家在群里讨论为主 召开时长10分钟 任务表格 袁勤 负责整理实验报告前后端交互,即xml文件传值部分 负责整理实验报告前后端交互 ...
- 通过ldap验证svn服务
1.简单介绍: 这里需要介绍一点的就是svn服务器的验证是需要通过SASL机制的,那么SASL全称为(Simple Authentication and security Layer),是一种用来扩充 ...
- leetCode191. 位1的个数
编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量) 示例 1: 输入:00000000000000000000000000001011 输出:3 解 ...
- 初识Git->GitHub
这不是一篇教程,这是学习过程的一个记录,初次使用GitHub的小白请移步文章末尾的参考链接, 先了解Git是什么东西 Git的工作流程 操作Git 练习使用 边用边学 #--------------- ...