存储过程使用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 ([email protected] or @DataSource=‘ALL‘)
            --and ([email protected] or @BatchNum=‘ALL‘)
            --and ([email protected] 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)+‘3‘+right(splitbatchnum,4) where currencycode=‘HKD‘ and (Extracted=‘Y‘ or Extracted=‘P‘)
    update #temp_SplitBatch set splitbatchnum=left(splitbatchnum,len(splitbatchnum)-5)+‘4‘+right(splitbatchnum,4) where currencycode=‘USD‘ and (Extracted=‘Y‘ or Extracted=‘P‘)
    update #temp_SplitBatch set splitbatchnum=left(splitbatchnum,len(splitbatchnum)-5)+‘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=‘0‘
    End

    ;With Batch as(
    select DataSourceID,PVBatch.BatchNum,CurrentProcess,BatchStatus,TotalChequeCount,Extracted,currencycode

            from PVBatch
            where (DataSourceID=@DataSource or @DataSource=‘0‘)
            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)+‘3‘+right(PVBatch.BatchNum,4)
               When PVITem.currencycode=‘USD‘ and (PVBatch.Extracted=‘Y‘ or PVBatch.Extracted=‘P‘) Then left(PVBatch.BatchNum,len(PVBatch.BatchNum)-5)+‘4‘+right(PVBatch.BatchNum,4)
               When PVITem.currencycode=‘CNY‘ and (PVBatch.Extracted=‘Y‘ or PVBatch.Extracted=‘P‘) Then left(PVBatch.BatchNum,len(PVBatch.BatchNum)-5)+‘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=‘0‘)
            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

时间: 2024-10-08 09:33:12

存储过程使用CTE 和 case when的相关文章

(转载) 两个数据库比较 对比视图存储过程及表结构差异

一.视图和存储过程比较 [原理]利用系统表"sysobjects"和系统表"syscomments",将数据库中的视图和存储过程进行对比.系统表"sysobjects"之前有详细介绍过,有兴趣可以看看:SQL Server系统表sysobjects介绍与使用 [代码] /*--调用示例 exec p_compdb 'DBNAME1','DBNAME2' exec p_compdb 'DBNAME2','DBNAME3' --*/ CREATE p

比较两个数据库中的视图/存储过程的结构(结构比较,不是功能比较)

CREATE PROC P_COMPDB @DB1 SYSNAME, --第一个库 @DB2 SYSNAME --第二个库 AS EXEC(' SELECT 类型=CASE ISNULL(A.XTYPE,B.XTYPE) WHEN ''V'' THEN ''视图'' ELSE ''存储过程'' END ,匹配情况=CASE WHEN A.NAME IS NULL THEN ''库 ['[email protected]+'] 中无'' WHEN B.NAME IS NULL THEN ''库 [

两个数据库比较 对比视图存储过程及表结构差异

两个数据库比较 对比视图存储过程及表结构差异 一.视图和存储过程比较 [原理]利用系统表“sysobjects"和系统表“syscomments”,将数据库中的视图和存储过程进行对比.系统表"sysobjects"之前有详细介绍过,有兴趣可以看看:SQL Server系统表sysobjects介绍与使用 如果你看到这段文字,说明您正使用RSS阅读或转自<一棵树-博客园>,原文地址:http://www.cnblogs.com/atree/p/db-compare-

QL Server 实用脚本

use MyFirstDB; -- 主要内容 -- SQL Server 实用脚本 -- 1.case语句 -- 2.子查询 -- 3.连接查询 -- 4.脚本变量与流程控制(选择与循环等) -- 5.事务 -- 6.存储过程 -- 7.触发器 --------------------------------- -- case 语句 -- 类似于C#中的三元表达式,好比n元表达式 -- 语法 -- 1)写在哪里? 在sql语句中需要值(标量)的地方 -- 2) -- if-else结构 /* c

递归查询树,并绑定到界面控件

1.环境:VS2013 SQL Server2012 2.数据库脚本: with CTE as ( -->Begin 一个定位点成员 select Id,MenuName,ParentId,cast(MenuName as nvarchar(max)) as TE,OrderNum, --ROW_NUMBER()over(order by getdate()) as OrderID CAST( (CASE WHEN OrderNum < 10 THEN '00'+LTRIM(OrderNum)

sql2(约束、日期、isnull、case、exists、cast\convert、索引、视图、存储过程、触发器、备份与还原)

1 . Primary Key 约束 SQLServer 中有五种约束, Primary Key 约束. Foreign Key 约束. Unique 约束. Default 约束和 Check 约束. 在表中常有一列或多列的组合,其值能唯一标识表中的每一行. 这样的一列或多列成为表的主键(PrimaryKey).一个表只能有一个主键,而且主键约束中的列不能为空值.只有主键列才能被作为其他表的外键所创建. 创建主键约束可以右键单击表,选择设计 . 选中要创建主键的列,然后单击上面的小钥匙. 也可

2015-10-20 SQL 第二次课 (约束、日期、isnull、case、exists、cast\convert、索引、视图、存储过程、触发器、备份与还原)

1 . Primary Key 约束 SQLServer 中有五种约束, Primary Key 约束. Foreign Key 约束. Unique 约束. Default 约束和 Check 约束. 在表中常有一列或多列的组合,其值能唯一标识表中的每一行. 这样的一列或多列成为表的主键(PrimaryKey).一个表只能有一个主键,而且主键约束中的列不能为空值.只有主键列才能被作为其他表的外键所创建. 创建主键约束可以右键单击表,选择设计 . 选中要创建主键的列,然后单击上面的小钥匙. 也可

存储过程里面的CASE,WHEN,THEN的用法

方法1:CASE WHEN 条件1 THEN 结果1 WHEN 条件2 THEN 结果2... WHEN 条件i THEN 结果i ELSE 默认结果END方法2:CASE 字段/变量WHEN 值1 THEN 结果1 WHEN 值2 THEN 结果1... WHEN 值i THEN 结果1 ELSE 默认结果END用法:比如查询:SELECT 字段1,(CASE ... END) FROM 表WHERE 字段1 = (CASE ... END) SQL中的case when使用小例 用一个SQL

四、Oracle loop循环、while循环、for循环、if选择和case选择、更改读取数据、游标、触发器、存储过程

数据库的设计(DataBase Design): 针对于用户特定的需求,然后我们创建出来一个最使用而且性能高的数据库! 数据库设计的步骤: 01.需求分析 02.概念结构设计 03.逻辑结构设计 04.物理结构设计 05.数据库的实施 06.数据库的运行和维护 数据库的3大范式: 1.确保每列的原子性!每一列都是一个不可再分的数据! 2.确保每列都和主键相关! 3.确保每列都和主键有直接的管理,而不是间接依赖(传递依赖)! PL/SQL: (Procedural Language) 过程化sql