Tuning SQL via case when statement
原SQL如下:SQL的主要问题是红色部分居然通过标量查询,反复的查找与SQL相同的基表,很显然这个可以用case when来简化。 select
a.TRAN_ID,
a.AMOUNT,
a.BALANCE,
a.INVAMT,
a.PROMISED,
a.INVNO,
a.RCLNUM,
b.PROBLEM_ID,
a.TRANTYPE,
a.TYPE,
a.DUEDATE,
a.INVDATE,
a.RCLDATE,
a.LASTTYPE,
a.LOCBAL,
a.CUSTNO,
b.STATUS,
a.PAYMENTS_PENDING,
isnull(( SELECT 'Y' FROM GPCOMP1.GPRECL aa with (NOLOCK) LEFT OUTER JOIN GPCOMP1.GPPROB b with (NOLOCK) ON (aa.tran_id = b.open_invoice_tran_id), GPCOMP1.GPTRCTRL c with (NOLOCK) WHERE aa.tran_id = a.tran_id AND aa.trantype = c.trantype AND ( ( c.CAPPLSTRAT = 'Y' AND (b.PROBLEM_ID IS NULL OR (b.PROBLEM_ID IS NOT NULL AND b.STATUS = 'C' )) AND ((c.DAPPLSTRAT = 'Y' AND c.CAPPLSTRAT = 'Y') OR C.SAPPLYSTRAT='Y' OR (C.SAPPLYSTRAT='N' AND aa.INVNO not in (select distinct (invno) from GPCOMP1.GPRECL r with (NOLOCK) where tran_id = (select open_invoice_tran_id from GPCOMP1.GPPROB p with (NOLOCK) where p.open_invoice_tran_id = r.tran_id)))) ) OR ( c.DAPPLSTRAT = 'Y' AND (b.PROBLEM_ID IS NOT NULL AND b.STATUS <> 'C' ) ) ) AND ( ( c.PPAPPLSTRAT = 'Y' AND aa.PAYMENTS_PENDING = 'Y' ) OR ( c.NPPAPPLSTRAT = 'Y' AND aa.PAYMENTS_PENDING != 'Y' ) ) ),'N')
as STRATEGIC from GPCOMP1.GPRECL a with (NOLOCK) LEFT OUTER JOIN GPCOMP1.GPPROB b with (NOLOCK) on (b.OPEN_INVOICE_TRAN_ID = a.TRAN_ID) where a.CUSTNO= @P0 order by a.INVNO
changed to
select
a.TRAN_ID,
a.AMOUNT,
a.BALANCE,
a.INVAMT,
a.PROMISED,
a.INVNO,
a.RCLNUM,
b.PROBLEM_ID,
a.TRANTYPE,
a.TYPE,
a.DUEDATE,
a.INVDATE,
a.RCLDATE,
a.LASTTYPE,
a.LOCBAL,
a.CUSTNO,
b.STATUS,
a.PAYMENTS_PENDING,
CASE
WHEN c.PPAPPLSTRAT = 'Y'
AND a.PAYMENTS_PENDING = 'Y'
AND c.DAPPLSTRAT = 'Y'
AND (b.PROBLEM_ID IS NOT NULL AND b.STATUS <> 'C' )
THEN 'Y'
WHEN c.PPAPPLSTRAT = 'Y'
AND a.PAYMENTS_PENDING = 'Y'
AND c.CAPPLSTRAT = 'Y'
AND (b.PROBLEM_ID IS NULL OR (b.PROBLEM_ID IS NOT NULL AND b.STATUS = 'C' ))
AND (c.DAPPLSTRAT = 'Y' or C.SAPPLYSTRAT='Y' or (C.SAPPLYSTRAT='N' AND b.open_invoice_tran_id is null))
THEN 'Y'
WHEN c.NPPAPPLSTRAT = 'Y'
AND a.PAYMENTS_PENDING != 'Y'
AND c.DAPPLSTRAT = 'Y'
AND (b.PROBLEM_ID IS NOT NULL AND b.STATUS <> 'C' )
THEN 'Y'
WHEN c.NPPAPPLSTRAT = 'Y'
AND a.PAYMENTS_PENDING != 'Y'
AND c.CAPPLSTRAT = 'Y'
AND (b.PROBLEM_ID IS NULL OR (b.PROBLEM_ID IS NOT NULL AND b.STATUS = 'C' ))
AND (c.DAPPLSTRAT = 'Y' or C.SAPPLYSTRAT='Y' or (C.SAPPLYSTRAT='N' and b.open_invoice_tran_id is null))
THEN 'Y'
ELSE 'N'
END as STRATEGIC
FROM GPCOMP1.GPRECL a
LEFT OUTER JOIN GPCOMP1.GPPROB b on (b.OPEN_INVOICE_TRAN_ID = a.TRAN_ID)
LEFT OUTER JOIN GPCOMP1.GPTRCTRL c on (c.trantype = a.trantype)
where a.CUSTNO= '' order by a.INVNO
Tuning SQL via case when statement的更多相关文章
- SQL利用Case When Then多条件判断SQL 语句
http://www.cnblogs.com/kevin2013/archive/2010/07/02/1769682.html SQL利用Case When Then多条件判断SQL ,用于sele ...
- 关于sql的case when用法简述
刚入手公司项目,需要添加一个功能,用到了SQL的case when以及concat SELECT eve.cc, eve.sc, case concat(cc,sc) ' THEN '' ' THEN ...
- SQL Fundamentals: Basic SELECT statement基本的select语句(控制操作的现实列)(FROM-SELECT)
SQL Fundamentals || Oracle SQL语言 Capabilities of the SELECT Statement(SELECT语句的功能) Data retrieval fr ...
- SQL之case when then用法
case具有两种格式.简单case函数和case搜索函数. 按 Ctrl+C 复制代码 这两种方式,可以实现相同的功能.简单case函数的写法相对比较简洁,但是和case搜索函数相比,功能方面会有些限 ...
- sql中case when语句的使用-来自网摘文章
Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...
- SQL中case when then用法
sql语句判断方式之一Case.具有两种格式:简单的Case函数.Case搜索函数. 1.简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' E ...
- SQL利用Case When Then多条件判断
CASE WHEN 条件1 THEN 结果1 WHEN 条件2 THEN 结果2 WHEN 条件3 THEN 结果3 WHEN 条件4 THEN 结果4 ....... ...
- sql 中 case when 语法(转)
sql语言中有没有类似C语言中的switch case的语句?? 没有,用case when 来代替就行了. 例如,下面的语句显示中文年月 select ...
- SQL中Case的使用方法(下篇)(转)
接上篇 四,根据条件有选择的UPDATE. 例,有如下更新条件 工资5000以上的职员,工资减少10% 工资在2000到4600之间的职员,工资增加15% 很容易考虑的是选择执行两次UPDATE语句, ...
随机推荐
- JavaScript事件大全3
//无模式的提示框 //屏蔽按键 <html> <head> <meta http-equiv="Content-Type" content=& ...
- 关于SparkMLlib的基础数据结构 Spark-MLlib-Basics
此部分主要关于MLlib的基础数据结构 1.本地向量 MLlib的本地向量主要分为两种,DenseVector和SparseVector,顾名思义,前者是用来保存稠密向量,后者是用来保存稀疏向量,其创 ...
- Qt中QUdpSocket序列化问题
写了一个小的Qt网络程序,很简单,发送的网络消息除了字符串还有一个结构体.很简单的想到用memcpy()函数来将数据序列化为BYTE数组从而实现网络传输. 序列化是Java中一个概念,C中并没有,C+ ...
- Html - 仿Ios assistiveTouch 悬浮辅助球工具
仿Ios assistiveTouch 悬浮辅助球工具 <!DOCTYPE html> <html> <head> <meta charset="u ...
- [ZZ] HDR&ToneMapping
http://blog.csdn.net/toughbro/article/details/6745207 float游戏存储照片blogimage HDR high dynamic range. 很 ...
- 转载~vim配置文件
转载自:小码哥的博客 花了很长时间整理的,感觉用起来很方便,共享一下. 我的vim配置主要有以下优点: 1.按F5可以直接编译并执行C.C++.java代码以及执行shell脚本,按“F8”可进行C. ...
- PHP不仅仅是PHP
PHP不仅仅是PHP PHP不仅仅是PHP. PHP的面试不仅仅会问到PHP语言本身(基本都是基础和细节),下面列举的主要是都是高级工程师的要求 比如:PHP中include和require的区别 ...
- Ecplise + Xdebug 一波三折终于能单步调试了
http://my.oschina.net/012345678/blog/152889 Ecplise + Xdebug 一波三折终于能单步调试了 发表于2年前(2013-08-15 15:50) ...
- C# vba 操作 Word
添加引用 Microsoft Word *.0 Object Library Microsoft Graph *.0 Object Library 变量说明 Object oMissing = Sy ...
- phpstorm9 无法输入中文逗号句号等符号了,怎么破?
最近手贱把phpstorm 升级到了最新版,发现输入中文符号输入不了呀,全部都变成英文符号了,例如输入的逗号.句号(,.)等都被转换成了(,.) 经过各方搜索,这个在官方也说了,是个bug,JDK的b ...