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= ‘12345‘  order by a.INVNO

Tuning SQL via case when statement

时间: 2024-10-05 15:31:28

Tuning SQL via case when statement的相关文章

关于sql的case when用法简述

刚入手公司项目,需要添加一个功能,用到了SQL的case when以及concat SELECT eve.cc, eve.sc, case concat(cc,sc) WHEN '00' THEN '' WHEN '10' THEN '得意先' WHEN '01' THEN '仕入先' WHEN '11' THEN '得意先/仕入先' ELSE '' END as client_supplier_class FROM ( SELECT bd0.id, bd0.created, bd0.creat

SQL Fundamentals: Basic SELECT statement基本的select语句

Basic SELECT statement基本的select语句 The basic syntax for a SELECT statement is presented below. SELECT语句的基本语法如下. |:多选一 []:可选择的内容 {}:多选一 没有被{}括起来的是必选 SELECT [DISTINCT | ALL] {* | select_list} FROM {table_name [alias] | view_name}     [{table_name [alias

SQL之case when then用法

case具有两种格式.简单case函数和case搜索函数. --简单case函数 case sex when '1' then '男' when '2' then '女' else '其他' end --case搜索函数 case when sex = '1' then '男' when sex = '2' then '女' else '其他' end 这两种方式,可以实现相同的功能.简单case函数的写法相对比较简洁,但是和case搜索函数相比,功能方面会有些限制,比如写判定式. 还有一个需要

SQL之case when then用法(用于分类统计)

ase具有两种格式.简单case函数和case搜索函数. --简单case函数 case sex when '1' then '男' when '2' then '女’ else '其他' end --case搜索函数 case when sex = '1' then '男' when sex = '2' then '女' else '其他' end 这两种方式,可以实现相同的功能.简单case函数的写法相对比较简洁,但是和case搜索函数相比,功能方面会有些限制,比如写判定式. 还有一个需要注

SQL Fundamentals SQL列字段的处理 SQL(case、decode、行转列)

列字段的处理 SQL Case 对列做处理 SQL> select deptno,sum(sal) from emp group by deptno; Select case when deptno=10 then 'ACCOUNTING' when deptno=20 then 'RESERCH' when deptno=30 then 'SALES' end, sum(sal) from emp group by deptno 列字段的处理 SQL Decode 还有一种情况是当什么都不是的

SQL的case when then else end语句的用法

SELECT a.managecom, a.subtype, count(*) loadsucc, sum(case when a.state in ('4', '5', '6', '7', '8', '9') then 1 else 0 end) recogsucc, sum(case when a.state in ('3', '12', '13') then 1 else 0 end) recogfail, sum(case when a.state in ('1', '2') then

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); ins

sql中case when语句的使用

case when语句有两种格式:简单case函数和搜索case函数. --简单Case函数CASE sexWHEN '1' THEN '男'WHEN '2' THEN '女'ELSE '其他' END--Case搜索函数CASE WHEN sex = '1' THEN '男'WHEN sex = '2' THEN '女'ELSE '其他' END 这两种方式,可以实现相同的功能.简单Case函数的写法相对比较简洁,但是和Case搜索函数相比,功能方面会有些限制,比如写判断式. 还有一个需要注意

SQL中case when then用法

sql语句判断方式之一Case.具有两种格式:简单的Case函数.Case搜索函数. 1.简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END 2.Case搜索函数 CASE WHEN sex='1' THEN '男' WHEN sex='2' THEN '女' ELSE sex='其他' END