存储过程使用CTE 和 case when
未用SQL CTE and case when:
ALTER PROCEDURE [dbo].[usp_rptDropboxBatchSummary1]
@DataSource varchar(10)='ALL',
@BatchNum varchar(8)='ALL',
@CurrentProcess varchar(10)='ALL'
AS
BEGIN
SET NOCOUNT ON;
--select * from PVBatch
--select * from pvitem
--0.set the source table
select IDENTITY(int,1,1) as id,DataSourceID,BatchNum,CurrentProcess,BatchStatus,TotalChequeCount,0 as RejectChequeCount,0 as AcceptChequeCount,
0 as HostPostedCount,convert(varchar(10),'') as SplitBatchNum,Currencycode,0 as SplitBatchAcceptCount,convert(decimal(18,2), 0) as SplitBatchClearingChqAmt,Extracted
into #temp_RptBatchs from PVBatch with(nolock) where 1=1
--where (DataSourceID=@DataSource or @DataSource='ALL')
--and (BatchNum=@BatchNum or @BatchNum='ALL')
--and (CurrentProcess=@CurrentProcess or @CurrentProcess='ALL')
--1.get the conditon data from PVItem
select i.BatchNum,i.CurrencyCode,ItemStatus,HostPostStatus,isnull(ClearingChqAmt,0) as ClearingChqAmt,b.Extracted into #temp_batchItems from PVItem i with(nolock)
join PVBatch b on i.batchnum=b.batchnum
where ItemType='C' and i.batchnum in (select batchnum from #temp_RptBatchs)
--2.get the RejectChequeCount and AcceptChequeCount and HostPostedCount
--3.set the RejectChequeCount
update a set a.RejectChequeCount=b.Rcount from #temp_RptBatchs a,
(select count(*) as Rcount,batchnum from #temp_batchItems where itemstatus='Reject' group by batchnum) b where a.batchnum=b.batchnum
--4.get the AcceptChequeCount
--5.set the AcceptChequeCount
update a set a.AcceptChequeCount=b.Rcount from #temp_RptBatchs a,
(select count(*) as Rcount,batchnum from #temp_batchItems where itemstatus='Accept' group by batchnum) b where a.batchnum=b.batchnum
--6.get the HostPostedCount
--7.set the HostPostedCount
update a set a.HostPostedCount=b.Rcount from #temp_RptBatchs a,
(select count(*) as Rcount,batchnum from #temp_batchItems where HostPostStatus='P' group by batchnum) b where a.batchnum=b.batchnum
--8.to get the split batch table group by the currency code
select batchNum,batchNum as SplitBatchNum,CurrencyCode,sum(ClearingChqAmt) as SplitClearingChqAmt,0 as SplitBatchAcceptCount,Extracted into #temp_SplitBatch
from #temp_batchItems group by CurrencyCode,batchNum,Extracted
--9.set the SplitBatchAcceptCount
update a set a.SplitBatchAcceptCount=b.AcceptCurrency from
(select batchNum,count(itemstatus) as AcceptCurrency,CurrencyCode from #temp_batchItems where itemstatus='Accept' group by CurrencyCode,batchNum) as b,#temp_SplitBatch a
where a.batchnum=b.batchnum and a.CurrencyCode=b.CurrencyCode
--10.process the splitbatchnum(120001 --> 130001 (HKD)140001 (USD)150001 (CNY) update the back fifth)
update #temp_SplitBatch set splitbatchnum=left(splitbatchnum,len(splitbatchnum)-5)+''+right(splitbatchnum,4) where currencycode='HKD' and (Extracted='Y' or Extracted='P')
update #temp_SplitBatch set splitbatchnum=left(splitbatchnum,len(splitbatchnum)-5)+''+right(splitbatchnum,4) where currencycode='USD' and (Extracted='Y' or Extracted='P')
update #temp_SplitBatch set splitbatchnum=left(splitbatchnum,len(splitbatchnum)-5)+''+right(splitbatchnum,4) where currencycode='CNY' and (Extracted='Y' or Extracted='P')
--11.update and insert the split data into the Rpt table
--select * from #temp_RptBatchs
--select * from #temp_SplitBatch
Select * from #temp_RptBatchs left join #temp_SplitBatch on #temp_RptBatchs.BatchNum = #temp_SplitBatch.BatchNum
drop table #temp_RptBatchs
drop table #temp_batchItems
drop table #temp_SplitBatch
END
使用SQL CTE and case when:
ALTER PROCEDURE [dbo].[usp_rptDropboxBatchSummary]
@DataSource varchar(10)='ALL',
@BatchNum varchar(8)='ALL',
@CurrentProcess varchar(10)='ALL'
AS
BEGIN IF @DataSource='ALL'
Begin
Set @DataSource=''
End ;With Batch as(
select DataSourceID,PVBatch.BatchNum,CurrentProcess,BatchStatus,TotalChequeCount,Extracted,currencycode from PVBatch
where (DataSourceID=@DataSource or @DataSource='')
and (isnull(@BatchNum,'')='' or BatchNum=@BatchNum or @BatchNum='ALL')
and (CurrentProcess=@CurrentProcess or @CurrentProcess='ALL')
),
Item as(
select BatchNum
,Sum(Case When itemstatus='Reject' Then 1 Else 0 End) as RejectCount
,Sum(Case When itemstatus='Accept' Then 1 Else 0 End) as AcceptCount
,Sum(Case When HostPostStatus='P' Then 1 Else 0 End) as HostPostCount from PVITem
Group By BatchNum
),
PItem as(
select PVBatch.BatchNum ,PVITem.currencycode,PVBatch.Extracted,
sum(ClearingChqAmt) as SplitClearingChqAmt
, Case When PVITem.currencycode='HKD' and (PVBatch.Extracted='Y' or PVBatch.Extracted='P') Then left(PVBatch.BatchNum,len(PVBatch.BatchNum)-5)+''+right(PVBatch.BatchNum,4)
When PVITem.currencycode='USD' and (PVBatch.Extracted='Y' or PVBatch.Extracted='P') Then left(PVBatch.BatchNum,len(PVBatch.BatchNum)-5)+''+right(PVBatch.BatchNum,4)
When PVITem.currencycode='CNY' and (PVBatch.Extracted='Y' or PVBatch.Extracted='P') Then left(PVBatch.BatchNum,len(PVBatch.BatchNum)-5)+''+right(PVBatch.BatchNum,4)
else PVBatch.BatchNum
End as SplitBatchNum
,Sum(Case When itemstatus='Accept' then 1 else 0 end) as SplitAcceptCount
from PVBatch Left join PVITem
on PVBatch.BatchNum = PVItem.BatchNum
where (PVBatch.DataSourceID=@DataSource or @DataSource='')
and (@BatchNum='ALL' or PVBatch.BatchNum=@BatchNum or isnull(@BatchNum,'')='')
and (PVBatch.CurrentProcess=@CurrentProcess or @CurrentProcess='ALL') Group By PVBatch.BatchNum,PVITem.currencycode,PVBatch.Extracted
) select
Batch.*, IsNull(Item.RejectCount,0) RejectCount, IsNull(Item.AcceptCount,0) AcceptCount, IsNull(Item.HostPostCount,0) HostPostCount
, Isnull(PITem.currencycode, Batch.currencycode) currencycode, Isnull(PITem.SplitClearingChqAmt,0) SplitClearingChqAmt,PITem.SplitAcceptCount, PItem.SplitBatchNum
, Case When row_number() over( PARTITION BY Batch. BatchNum order by SplitBatchNum) = 1 then 0 else 1 end groupflag
from Batch Left join Item
on Batch. BatchNum = ITem. BatchNum
Left join PITem
on Batch. BatchNum = PITem.BatchNum
order by Batch. BatchNum,PItem.SplitBatchNum End
结果集:
存储过程使用CTE 和 case when的更多相关文章
- 调用MYSQL存储过程实例
PHP调用MYSQL存储过程实例 http://blog.csdn.net/ewing333/article/details/5906887 http://www.cnblogs.com/kkchen ...
- mysql存储过程--学习
-- 存储过程示例一 inDROP DATABASE IF EXISTS tdemo;CREATE DATABASE tdemo CHARACTER SET=utf8; USE tdemo;CRE ...
- Sql Server 数据库表结构,存储过程,视图比较脚本
顶级干货 用来比较两个数据库之间 表结构,存储过程及视图差异的存储过程,直接复制对应的存储过程,无需改动,直接在数据库中执行(传递要比较的数据库参数)即可 1.两个数据库之间存储过程及视图差异比较的存 ...
- QL Server 实用脚本
use MyFirstDB; -- 主要内容 -- SQL Server 实用脚本 -- 1.case语句 -- 2.子查询 -- 3.连接查询 -- 4.脚本变量与流程控制(选择与循环等) -- 5 ...
- 金蝶K3 WISE BOM多级展开_BOM成本表
/****** Object: StoredProcedure [dbo].[pro_bobang_BOMCost] Script Date: 07/29/2015 16:09:11 ******/ ...
- 六、K3 WISE 开发插件《直接SQL报表开发新手指导 - BOM成本报表》
======================== 目录: 1.直接SQL报表 ======================== 1.直接SQL报表 以BOM成本报表为例,在销售模块部署,需要购买[金蝶 ...
- 为什么不能用drop function add 去删除函数? 因为不能使用 mysql中的保留字!
mysql中有很多的 保留字, 也叫关键字, 你在使用 数据库中的任何东西, 都最好是 避开这些关键字/保留字, 包括 数据库名, 表名, 字段名, 函数名, 存储过程名. 这些关键字包括: mysq ...
- SQL Server 对比数据库差异
一.视图和存储过程比较 [原理]利用系统表“sysobjects"和系统表“syscomments”,将数据库中的视图和存储过程进行对比.系统表"sysobjects"之 ...
- [翻译]MySQL 文档: Control Flow Functions(控制流函数)
本文翻译自13.4 Control Flow Functions Table 13.6 Flow Control Operators 名称 描述 CASE Case 运算符 IF() if/else ...
随机推荐
- VS2010创建动态链接库并且使用动态链接库DLL
1.编写动态链接库文件 dll和lib文件 例子: 在新建VS工程时选择DLL 空项目 ----------hello.h-------- #include <stdio.h> #prag ...
- centos systemctl指令
# systemctl #输出已激活单元 # systemctl list-units #输出已激活单元 # systemctl --failed #输出运行失败的单元 # systemctl lis ...
- Javascript高级程序设计——基本概念(二)
相等操作符: 相等==:这个操作符会先转换操作数,强制类型转换,然后再比较他们的相等性. null == undefined //true NaN == NaN //false"5" ...
- hexo问题篇(偶尔抽抽疯)
hexo安安稳稳的跑了很久,然后 ....让人心碎的hexo问题,华丽丽的摔倒在坑里,只因update了hexo version最是哪一句 hexo server让人欲哭无泪 -问题场景 设备: Ma ...
- Android Sqlite 数据库版本更新
Android Sqlite 数据库版本更新 http://87426628.blog.163.com/blog/static/6069361820131069485844/ 1.自己写一个类继承 ...
- 实战Centos系统部署Codis集群服务
导读 Codis 是一个分布式 Redis 解决方案, 对于上层的应用来说, 连接到 Codis Proxy 和连接原生的 Redis Server 没有明显的区别 (不支持的命令列表), 上层应用可 ...
- Longest Common Subsequence
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- HTML5 localStorage 的使用
在前端开发中,我们会常遇到要在两个甚至多个html之间通信,我们可以在url中添加参数,但是当要传递的数据量较大较多时呢,不妨试试html5 的localStorage吧. 1) 检测你的浏览器是 ...
- 学号160809224姓名黄家帅c语言程序设计实验2 选择结构程序设计
实验2-1 输入3个数,并按由大到小的顺序输出. 实验要求: 编写一个C程序,输入3个数,并按由大到小的顺序输出. 源码: #include <stdio.h>void main(){ i ...
- ubuntu下如何用命令行运行deb安装包
如果ubuntu要安装新软件,已有deb安装包(例如:iptux.deb),但是无法登录到桌面环境.那该怎么安装?答案是:使用dpkg命令. dpkg命令常用格式如下: sudo dpkg -I ip ...