在 Sql server 中递归分布行

Posted

技术标签:

【中文标题】在 Sql server 中递归分布行【英文标题】:Recursively Distribute Rows in Sql server 【发布时间】:2015-03-05 08:00:09 【问题描述】:

我无法设置一个查询,该查询将用于从现有行中获取其他行的信息。

现有表数据

在上图中,RecptNo 20100001182 有三行。

在 Recptno 20100001182 我有三个检查 分别检查 Instno 5,6,7 的 Nos 000005,000006,000007

recptno 20100001182 的检查使用 Recptno 20100001970 返回

但是 recptno 20100001970 没有捕获 instno 所以需要在两个 recpt 中分发匹配检查号。

期望的输出

当反弹或负输入完成时,由 负金额和类型=“O”。 -ve 金额应针对在该日期之前存在且具有相同 chqno(如果是支票)或没有支票号码的现有 +ve 金额行进行分配 应该先选择最高 Instno 的数量,然后再下降 instno 直到分配总金额。

现在在 RecptNo 20100001971 中收到金额以掩盖退回的支票。

RecptNo 20100001971 中收到的金额需要适当分配。现在当 Type="O" 且金额为正数时,我们应该先选择最小 Instno,然后按升序选择余数,直到正确划分总金额。

期望的结果应该如下。

我尝试过的 很复杂,可能是可读的

    DECLARE @sign as int
DECLARE @CHKREGNO AS VARCHAR(30)
DECLARE @PayModeID  varchar(30),
@StdledgerID    varchar(10),
@RecptKey   varchar(30),
@InstType   varchar(1),
@BaseAmount bigint,
@PayModeType    varchar(10),
@LedgerDate datetime,
@InstNo int,
@RegNo  varchar(30),
@PostDate   datetime,
@ServiceTax bigint,
@Dvat   bigint,
@Concession money,
@PaymentStatus  int,
@CenterID   varchar(10),
@ddChqCardNo    varchar(20),
@IsMarketing    bit,
@ThirdPartyTransDate    datetime,
@ThirdPartyPsnName  varchar(100),
@ThirdPartyTransLoc varchar(100),
@ThirdPartyName varchar(100),
@DDChqCardDt    datetime,
@Lumpsump   money,
@STaxPer    money,
@ConcessionWTax money,
@LumpsumpWTax   money,
@refInstno  int,
@refStdLedgerId int,
@refPayModeId   varchar(20),
@refConcession  money,
@refLumpsump    money,
@refConcessionDtl   money,
@refLumpsumpDtl money,
@STAX   decimal,
@RN INT 


DECLARE @PayModeID2 varchar(30),
@StdledgerID2   varchar(10),
@RecptKey2  varchar(30),
@InstType2  varchar(1),
@BaseAmount2    bigint,
@PayModeType2   varchar(10),
@LedgerDate2    datetime,
@InstNo2    int,
@RegNo2 varchar(30),
@PostDate2  datetime,
@ServiceTax2    bigint,
@Dvat2  bigint,
@Concession2    money,
@PaymentStatus2 int,
@CenterID2  varchar(10),
@ddChqCardNo2   varchar(20),
@IsMarketing2   bit,
@ThirdPartyTransDate2   datetime,
@ThirdPartyPsnName2 varchar(100),
@ThirdPartyTransLoc2    varchar(100),
@ThirdPartyName2    varchar(100),
@DDChqCardDt2   datetime,
@Lumpsump2  money,
@STaxPer2   money,
@ConcessionWTax2    money,
@LumpsumpWTax2  money,
@refInstno2 int,
@refStdLedgerId2    int,
@refPayModeId2  varchar(20),
@refConcession2 money,
@refLumpsump2   money,
@refConcessionDtl2  money,
@refLumpsumpDtl2    money,
@STAX2  decimal,
@RN2 INT, 
@finalamount    bigint,
@finalStax  bigint,
@finalDVat  bigint,
@base   money,
@setax  money,
@devat  money

SET @CHKREGNO='My Regno'
DECLARE @records CURSOR
SET @records= Cursor FOR select row_number() over (Order by regno,Ledgerdate,case when Insttype='R' then -2 when Insttype='A' then -1 when Insttype='I' then instno end) as rn,* from Payment_Ledger_Matching_bkp2 where regno=@CHKREGNO
OPEN @RECORDS
FETCH NEXT FROM @RECORDS INTO @RN,@PayModeID,@StdledgerID,@RecptKey,@InstType,@BaseAmount,@PayModeType,@LedgerDate,@InstNo,@RegNo,@PostDate,@ServiceTax
,@Dvat,@Concession,@PaymentStatus,@CenterID,@ddChqCardNo,@IsMarketing,@ThirdPartyTransDate  ,@ThirdPartyPsnName ,@ThirdPartyTransLoc,@ThirdPartyName
,@DDChqCardDt,@Lumpsump,@STaxPer,@ConcessionWTax,@LumpsumpWTax,@refInstno,@refStdLedgerId,@refPayModeId,@refConcession,@refLumpsump,@refConcessionDtl
,@refLumpsumpDtl,@STAX 

WHILE @@FETCH_STATUS=0
BEGIN 
 if @InstType='O' 
 begin
    set @sign=sign(@BaseAmount)
    --DECLARE RECORDS2 CURSOR FOR
    --  select * from Payment_Ledger_Matching_bkp2_New3     where regno=@regno and Ledgerdate<@LedgerDate and rn<@RN  and  BaseAmount>0  order by rn desc
    DECLARE RECORDS2 CURSOR dynamic  FOR
    SELECT row_number() over (Order by regno,Ledgerdate,case when Insttype='R' then -2 when Insttype='A' then -1 when Insttype='I' then instno end) as rn,*
    FROM Payment_Ledger_Matching_bkp2 AS BKP2
        WHERE RegNo=@RegNo and
        case when Insttype='R' then -2 when Insttype='A' then -1 when Insttype='I' then instno end
        <=(
        select MAX(INSTNO) from (select MAX(case when Insttype='R' then -2 when Insttype='A' then -1 when Insttype='I' then instno end) instno
        --,SUM(case when BaseAmount<0 then abs(BaseAmount)+abs(ServiceTax) else 0 end)
        --,SUM(case when BaseAmount>0 then BaseAmount+ServiceTax else 0 end)
         from Payment_Ledger_Matching_bkp2_NEW3 AS BKP2_NEW
        WHERE REGNO=@REGNO  AND CASE WHEN  ISNULL(@ddChqCardNo,'')='' THEN 1 when @PaymentStatus in (5,6,7) then 1 WHEN  @BaseAmount<0 and ISNULL(ddChqCardNo,'')=@ddChqCardNo THEN 1 WHEN @BaseAmount>0 then 1 ELSE 0 END=1
        and case when @paymentstatus in (5,6,7) then case when PaymentStatus=0 then 1 else 0 end else 1 end=1
        GROUP BY REGNO,case when Insttype='R' then -2 when Insttype='A' then -1 when Insttype='I' then instno end       
        HAVING case when @BaseAmount>0 then case WHEN SUM(bASEAMOUNT+ServiceTax)=0 THEN 1 when  (SUM(case when BaseAmount<0 then abs(BaseAmount)+abs(ServiceTax) else 0 end)>=SUM(case when BaseAmount>0 then BaseAmount+ServiceTax else 0 end) and SUM(case when BaseAmount<0 then abs(BaseAmount)+abs(ServiceTax) else 0 end)<>0) then 0 else 1 end else case when SUM(bASEAMOUNT+ServiceTax)>0 then 1 else 0 end end=1
        )as r
        ) 
        AND  CASE WHEN  ISNULL(@ddChqCardNo,'')='' THEN 1 when @PaymentStatus in (5,6,7) then 1 WHEN @BaseAmount<0 and ISNULL(ddChqCardNo,'')=@ddChqCardNo THEN 1 WHEN @BaseAmount>0 then 1 ELSE 0 END=1        
        and case when @paymentstatus in (5,6,7) then case when PaymentStatus=0 then 1 else 0 end else 1 end=1
        ORDER BY 
        case when @Baseamount>0
                then 10-case when Insttype='R' then -2 when Insttype='A' then -1 when Insttype='I' then 
                case when instno IN (
                            select MAX(case when Insttype='R' then -2 when Insttype='A' then -1 when Insttype='I' then instno end) instno
        --,SUM(case when BaseAmount<0 then abs(BaseAmount)+abs(ServiceTax) else 0 end)
        --,SUM(case when BaseAmount>0 then BaseAmount+ServiceTax else 0 end)
        --,SUM(bASEAMOUNT+ServiceTax)
         from Payment_Ledger_Matching_bkp2_NEW3 AS BKP2_NEW
        WHERE REGNO=@REGNO  AND CASE WHEN  ISNULL(@ddChqCardNo,'')='' THEN 1 when @PaymentStatus in (5,6,7) then 1 WHEN  @BaseAmount<0 and ISNULL(ddChqCardNo,'')=@ddChqCardNo THEN 1 WHEN @BaseAmount>0 then 1 ELSE 0 END=1
        GROUP BY REGNO,case when Insttype='R' then -2 when Insttype='A' then -1 when Insttype='I' then instno end       
        HAVING case when @BaseAmount>0 then 
                    CASE WHEN SUM(bASEAMOUNT+ServiceTax)=0 THEN 1 
                    WHEN  (SUM(case when BaseAmount<0 then abs(BaseAmount)+abs(ServiceTax) else 0 end)
                                <SUM(case when BaseAmount>0 then BaseAmount+ServiceTax else 0 end) 
                    and SUM(case when BaseAmount<0 then abs(BaseAmount)+abs(ServiceTax) else 0 end)=0)
                     then 0 else 1 end 
                     else 
                        case when SUM(bASEAMOUNT+ServiceTax)>0 then 1 else 0 end 
                    end=1
                            )
                        then -2*(10-Instno)
                        else Instno end
                end 
        --when ISNULL(ddChqCardNo,'')=@ddChqCardNo then  10-case when Insttype='R' then -2 when Insttype='A' then -1 when Insttype='I' then instno end 
        else  case when Insttype='R' then -2 when Insttype='A' then -1 when Insttype='I' then instno end 
        end DESC
        ,LedgerDate
    OPEN RECORDS2;
    FETCH NEXT FROM RECORDS2 INTO @RN2,@PayModeID2,@StdledgerID2,@RecptKey2,@InstType2,@BaseAmount2,@PayModeType2,@LedgerDate2,@InstNo2,@RegNo2,@PostDate2,@ServiceTax2
,@Dvat2,@Concession2,@PaymentStatus2,@CenterID2,@ddChqCardNo2,@IsMarketing2,@ThirdPartyTransDate2,@ThirdPartyPsnName2,@ThirdPartyTransLoc2,@ThirdPartyName2
,@DDChqCardDt2,@Lumpsump2,@STaxPer2,@ConcessionWTax2,@LumpsumpWTax2,@refInstno2,@refStdLedgerId2,@refPayModeId2,@refConcession2,@refLumpsump2,@refConcessionDtl2
,@refLumpsumpDtl2,@STAX2
    WHILE @@FETCH_STATUS = 0
    BEGIN       

        if abs(@BaseAmount)>0 
        begin

            select 
             @Base=SUM(BaseAmount)--SUM(bASEAMOUNT)
            ,@setax=SUM(ServiceTax)--SUM(ServiceTax)        
            ,@devat=SUM(dvat)--SUM(Dvat)        
         from Payment_Ledger_Matching_bkp2_NEW3 AS BKP2_NEW
        WHERE REGNO=@REGNO  AND CASE WHEN  ISNULL(@ddChqCardNo,'')='' THEN 1 when @PaymentStatus in (5,6,7) then 1 WHEN  @BaseAmount<0 and ISNULL(ddChqCardNo,'')=@ddChqCardNo THEN 1 WHEN @BaseAmount>0 then 1 ELSE 0 END=1        
        and Instno=@InstNo2
        GROUP BY REGNO,case when Insttype='R' then -2 when Insttype='A' then -1 when Insttype='I' then instno end       
        HAVING case when @BaseAmount>0 then 
                    CASE WHEN SUM(BASEAMOUNT+ServiceTax)=0 THEN 1 
                    WHEN  (SUM(case when BaseAmount<0 then abs(BaseAmount)+abs(ServiceTax) else 0 end)
                                <SUM(case when BaseAmount>0 then BaseAmount+ServiceTax else 0 end) 
                    and SUM(case when BaseAmount<0 then abs(BaseAmount)+abs(ServiceTax) else 0 end)=0)
                     then 0 else 1 end 
                     else 
                        case when SUM(BASEAMOUNT+ServiceTax)>0 then 1 else 0 end 
                    end=1
             DECLARE @tbase  as money,@tstax as money,@tdvat as money
                    select @tbase=SUM(case when ledgerdate<@ledgerdate then bASEAMOUNT else 0 end)
                    ,@tstax=SUM(case when ledgerdate<@ledgerdate then ServiceTax else 0 end)
                    ,@tdvat=SUM(case when ledgerdate<@ledgerdate then Dvat else 0 end)
                 from Payment_Ledger_Matching_bkp2 AS BKP2_NEW
                WHERE REGNO=@REGNO  AND CASE WHEN  ISNULL(@ddChqCardNo,'')='' THEN 1 when @PaymentStatus in (5,6,7) then 1 WHEN  @BaseAmount<0 and ISNULL(ddChqCardNo,'')=@ddChqCardNo THEN 1 WHEN @BaseAmount>0 then 1 ELSE 0 END=1
                and Instno=@InstNo2
                GROUP BY REGNO,case when Insttype='R' then -2 when Insttype='A' then -1 when Insttype='I' then instno end       

            if abs(@BaseAmount)>=@Base and  @base+@SeTAX+@Devat<@tBase+@tstax+@tdvat --and @@CURSOR_ROWS >1
            begin
                set @finalamount=CASE WHEN @tBase-@Base>@BASEAMOUNT THEN @BASEAMOUNT ELSE @tBase-@Base END
                set @BaseAmount=abs(@BaseAmount)-(@tBase-@Base)--CASE WHEN @tBase-@Base>@BASEAMOUNT THEN 0 ELSE abs(@BaseAmount)-(@tBase-@Base) END

                set @finalStax=CASE WHEN @tBase-@Base>@BASEAMOUNT THEN @ServiceTax ELSE @tstax-@SeTAX END
                set @ServiceTax=abs(@ServiceTax)-(@tstax-@SeTAX)--CASE WHEN @tBase-@Base>@BASEAMOUNT THEN 0 ELSE abs(@ServiceTax)-(@tstax-@SeTAX) END

                set @finalDVat=CASE WHEN @tBase-@Base>@BASEAMOUNT THEN @Dvat ELSE @tdvat-@Devat END
                set @Dvat=abs(@Dvat)-(@tdvat-@Devat)--CASE WHEN @tBase-@Base>@BASEAMOUNT THEN 0 ELSE abs(@Dvat)-(@tdvat-@Devat) END

                SET @Base=0
            end
            else if abs(@BaseAmount)<@Base and @base+@SeTAX+@Devat=@tBase+@tstax+@tdvat and @@CURSOR_ROWS >1 and @PaymentStatus not in (5,6,7)
            begin
            FETCH NEXT FROM RECORDS2 INTO @RN2,@PayModeID2,@StdledgerID2,@RecptKey2,@InstType2,@BaseAmount2,@PayModeType2,@LedgerDate2,@InstNo2,@RegNo2,@PostDate2,@ServiceTax2
,@Dvat2,@Concession2,@PaymentStatus2,@CenterID2,@ddChqCardNo2,@IsMarketing2,@ThirdPartyTransDate2,@ThirdPartyPsnName2,@ThirdPartyTransLoc2,@ThirdPartyName2
,@DDChqCardDt2,@Lumpsump2,@STaxPer2,@ConcessionWTax2,@LumpsumpWTax2,@refInstno2,@refStdLedgerId2,@refPayModeId2,@refConcession2,@refLumpsump2,@refConcessionDtl2
,@refLumpsumpDtl2,@STAX2
                continue;
            end
            else if abs(@BaseAmount)>=@BaseAmount2
            begin

                set @finalamount=@BaseAmount2
                set @BaseAmount=abs(@BaseAmount)-@BaseAmount2

                set @finalStax=@ServiceTax2
                set @ServiceTax=abs(@ServiceTax)-@ServiceTax2

                set @finalDVat=@Dvat2
                set @Dvat=abs(@Dvat)-@Dvat2

                SET @BaseAmount2=0
            end
            else
            begin
                set @finalamount=abs(@BaseAmount)
                SET @BaseAmount2=@BaseAmount2-@BaseAmount
                set @BaseAmount=0

                set @finalStax=@ServiceTax
                SET @ServiceTax2=@ServiceTax2-@ServiceTax
                set @ServiceTax=0

                set @finalDVat=@Dvat
                SET @Dvat2=@Dvat2-@Dvat
                set @Dvat=0


            end

            insert into Payment_Ledger_Matching_bkp2_New3 values
            (
                @PayModeID,@StdledgerID,@RecptKey2,@InstType2,@sign*@finalamount,@PayModeType
                ,@LedgerDate,@InstNo2,@RegNo2
                ,@PostDate,@sign*@finalStax,@sign*@finalDVat,@Concession2
                ,@PaymentStatus,@CenterID2,@ddChqCardNo,
                @IsMarketing2,@ThirdPartyTransDate2,@ThirdPartyPsnName2,@ThirdPartyTransLoc2,@ThirdPartyName2
                ,@DDChqCardDt
                ,@Lumpsump2,@STaxPer2,@ConcessionWTax2,
                @LumpsumpWTax2,@refInstno2,@refStdLedgerId,@refPayModeId2,@refConcession2,@refLumpsump2,@refConcessionDtl2,@refLumpsumpDtl2,@STAX2,@rn
            )

        end
        if(@BaseAmount=0) --BREAK;
        BEGIN
            IF (@BaseAmount2>0 ) and @@CURSOR_ROWS >1 and @BaseAmount2+@ServiceTax2+@Dvat2<>@Base+@setax+@devat
            BEGIN
                FETCH NEXT FROM @RECORDS INTO @RN,@PayModeID,@StdledgerID,@RecptKey,@InstType,@BaseAmount,@PayModeType,@LedgerDate,@InstNo,@RegNo,@PostDate,@ServiceTax,@Dvat,@Concession,@PaymentStatus,@CenterID,@ddChqCardNo,
@IsMarketing,@ThirdPartyTransDate   ,@ThirdPartyPsnName ,@ThirdPartyTransLoc    ,@ThirdPartyName    ,@DDChqCardDt   ,@Lumpsump  ,@STaxPer   ,@ConcessionWTax,
@LumpsumpWTax,@refInstno,@refStdLedgerId,@refPayModeId  ,@refConcession ,@refLumpsump,@refConcessionDtl,@refLumpsumpDtl,@STAX
                set @sign=sign(@BaseAmount)
            END             

            ELSE IF @BaseAmount<0 
            BEGIN

                FETCH NEXT FROM @RECORDS INTO @RN,@PayModeID,@StdledgerID,@RecptKey,@InstType,@BaseAmount,@PayModeType,@LedgerDate,@InstNo,@RegNo,@PostDate,@ServiceTax,@Dvat,@Concession,@PaymentStatus,@CenterID,@ddChqCardNo,
@IsMarketing,@ThirdPartyTransDate   ,@ThirdPartyPsnName ,@ThirdPartyTransLoc    ,@ThirdPartyName    ,@DDChqCardDt   ,@Lumpsump  ,@STaxPer   ,@ConcessionWTax,
@LumpsumpWTax,@refInstno,@refStdLedgerId,@refPayModeId  ,@refConcession ,@refLumpsump,@refConcessionDtl,@refLumpsumpDtl,@STAX
                set @sign=sign(@BaseAmount)
                CONTINUE;
            END     
            ELSE
            BEGIN
                BREAK;
            END

        END
        ELSE IF @BaseAmount<0 and @sign>0 and @@CURSOR_ROWS >1
        BEGIN
                FETCH NEXT FROM @RECORDS INTO @RN,@PayModeID,@StdledgerID,@RecptKey,@InstType,@BaseAmount,@PayModeType,@LedgerDate,@InstNo,@RegNo,@PostDate,@ServiceTax,@Dvat,@Concession,@PaymentStatus,@CenterID,@ddChqCardNo,
@IsMarketing,@ThirdPartyTransDate   ,@ThirdPartyPsnName ,@ThirdPartyTransLoc    ,@ThirdPartyName    ,@DDChqCardDt   ,@Lumpsump  ,@STaxPer   ,@ConcessionWTax,
@LumpsumpWTax,@refInstno,@refStdLedgerId,@refPayModeId  ,@refConcession ,@refLumpsump,@refConcessionDtl,@refLumpsumpDtl,@STAX
                set @sign=sign(@BaseAmount)
                CONTINUE;
        END;

            FETCH NEXT FROM RECORDS2 INTO @RN2,@PayModeID2,@StdledgerID2,@RecptKey2,@InstType2,@BaseAmount2,@PayModeType2,@LedgerDate2,@InstNo2,@RegNo2,@PostDate2,@ServiceTax2
,@Dvat2,@Concession2,@PaymentStatus2,@CenterID2,@ddChqCardNo2,@IsMarketing2,@ThirdPartyTransDate2,@ThirdPartyPsnName2,@ThirdPartyTransLoc2,@ThirdPartyName2
,@DDChqCardDt2,@Lumpsump2,@STaxPer2,@ConcessionWTax2,@LumpsumpWTax2,@refInstno2,@refStdLedgerId2,@refPayModeId2,@refConcession2,@refLumpsump2,@refConcessionDtl2
,@refLumpsumpDtl2,@STAX2
    END;
    CLOSE RECORDS2;
    DEALLOCATE RECORDS2;

 end
 else
 begin
    insert into Payment_Ledger_Matching_bkp2_New3 values
    (
        @PayModeID,@StdledgerID,@RecptKey,@InstType,@BaseAmount,@PayModeType,@LedgerDate,@InstNo,@RegNo,@PostDate,@ServiceTax,@Dvat,@Concession,@PaymentStatus,@CenterID,@ddChqCardNo,
        @IsMarketing,@ThirdPartyTransDate   ,@ThirdPartyPsnName ,@ThirdPartyTransLoc    ,@ThirdPartyName    ,@DDChqCardDt   ,@Lumpsump  ,@STaxPer   ,@ConcessionWTax,
        @LumpsumpWTax,@refInstno    ,@refStdLedgerId,@refPayModeId  ,@refConcession ,@refLumpsump,@refConcessionDtl,@refLumpsumpDtl,@STAX,@rn
    )
end
FETCH NEXT FROM @RECORDS INTO @RN,@PayModeID,@StdledgerID,@RecptKey,@InstType,@BaseAmount,@PayModeType,@LedgerDate,@InstNo,@RegNo,@PostDate,@ServiceTax,@Dvat,@Concession,@PaymentStatus,@CenterID,@ddChqCardNo,
@IsMarketing,@ThirdPartyTransDate   ,@ThirdPartyPsnName ,@ThirdPartyTransLoc    ,@ThirdPartyName    ,@DDChqCardDt   ,@Lumpsump  ,@STaxPer   ,@ConcessionWTax,
@LumpsumpWTax,@refInstno,@refStdLedgerId,@refPayModeId  ,@refConcession ,@refLumpsump,@refConcessionDtl,@refLumpsumpDtl,@STAX

END
close @RECORDS
deallocate @RECORDS;

select sum(Baseamount) over (partition by Regno),* from Payment_Ledger_Matching_bkp2_New3
SQLFiddle for Sample data to process SQlFiddle for desired output of sample of above sqlfiddle

【问题讨论】:

无法理解您的需求。到目前为止您的查询是什么?如何找到 recptno 20100001970? @HoneyBadger 实际上我代表的案例是针对一位用户的。所以显示的交易是关于一个用户的,交易主要是三种类型 I,O -ve 金额,O +ve 金额 @MaciejLos 我已经用我尝试过的方法更新了我的问题 @AmritpalSingh,仍然无法得到你的逻辑;(你想计算运行总和吗?如果 RcptNo=20100001971,你设计了什么? @MaciejLos 我们可以聊聊我会解释 【参考方案1】:

我只是在尝试我所理解的。所以不要拒绝投票。

最重要的是,我没有看到任何欲望输出。如果你提供,那么它将有助于快速理解。

我的理解,

    需要更新 InstType='O' 的 BaseAmount 以及您提到的所有其他条件。 在 InstType'O' 处无需更新 BaseAmount。它只有助于更新上面的第 1 点。像 'I'、'R'、'A' 这样的 InstType 在此要求中的用途相同。 根据您的解释,此要求只有两种类型的 InstType,即“O”和其他。

    所以如果上面是真的那么只有3个条件

    i) InstType 'O' with chequeNo ii) InstType 'O' with Negative BaseAmount iii) InstType 'O' with Positive BaseAmount

    所以基于 fiddle 示例数据。它可以在没有光标的情况下明确完成,并且可以进一步优化,只要事情更清楚。

;WITH ChequeCTE AS
 (
 SELECT ROW_NUMBER() over (PARTITION BY ddChqCardNo,iNSTTYPE Order by regno,Ledgerdate ) as rn
 ,[RecptKey], [InstType],[BaseAmount],[PayModeType],[LedgerDate],[InstNo],[Regno]
 ,[PostDate],[ServiceTax],[DVAT],[Concession],[PaymentStatus],[CenterId],[ddChqCardNo]
 ,[ddChqCardDt],[Lumpsump],[RowId],[concDtlsId],[concDiff]
 FROM @Matching_Table AS BKP2 WHERE  ISNUMERIC(ddChqCardNo)=1
 )
 ,ChequeCTE1 AS
 (
  SELECT RN,[RecptKey],
  [InstType],
  (SELECT B.[BaseAmount] FROM ChequeCTE B WHERE [InstType]<>'O' 
  AND A.RN=B.RN AND A.[ddChqCardNo]=B.[ddChqCardNo] )[BaseAmount],
  [PayModeType],[LedgerDate],[InstNo],[Regno],[PostDate],[ServiceTax],[DVAT]
  ,[Concession],[PaymentStatus],[CenterId],[ddChqCardNo],[ddChqCardDt],[Lumpsump],[RowId]
  ,[concDtlsId],[concDiff]
  FROM ChequeCTE A WHERE INSTTYPE='O' AND ISNUMERIC(ddChqCardNo)=1
 )
 ,NonChequeCTE_NotO_MinInstNo AS
 (
 SELECT ROW_NUMBER() over ( Order by regno ,[InstNo]  ) as rn,[RecptKey],
  [InstType],[BaseAmount],[PayModeType],[LedgerDate],[InstNo],[Regno],[PostDate]
  ,[ServiceTax],[DVAT]
  ,[Concession],[PaymentStatus],[CenterId],[ddChqCardNo],[ddChqCardDt],[Lumpsump]
  ,[RowId],[concDtlsId],[concDiff]
 FROM @Matching_Table AS BKP2 WHERE  ISNUMERIC(ddChqCardNo)=0 and [InstType]<>'O'
 )
 ,NonChequeCTE_NotO_MaxInstNo AS
 (
 SELECT ROW_NUMBER() over ( Order by regno ,[InstNo] desc  ) as rn,[RecptKey],
  [InstType],[BaseAmount],[PayModeType],[LedgerDate],[InstNo],[Regno],[PostDate]
  ,[ServiceTax],[DVAT]
  ,[Concession],[PaymentStatus],[CenterId],[ddChqCardNo],[ddChqCardDt],[Lumpsump]
  ,[RowId],[concDtlsId],[concDiff]
 FROM @Matching_Table AS BKP2 WHERE  ISNUMERIC(ddChqCardNo)=0 and [InstType]<>'O'
 )
 ,NonChequeCTE_O_PosAmount AS
 (
 SELECT ROW_NUMBER() over ( Order by regno,Ledgerdate ) as rn,[RecptKey],
  [InstType],[BaseAmount],[PayModeType],[LedgerDate],[InstNo],[Regno],[PostDate]
  ,[ServiceTax],[DVAT]
  ,[Concession],[PaymentStatus],[CenterId],[ddChqCardNo],[ddChqCardDt],[Lumpsump]
  ,[RowId],[concDtlsId],[concDiff]
 FROM @Matching_Table AS BKP2 WHERE  ISNUMERIC(ddChqCardNo)=0 and [InstType]='O' and [BaseAmount]>=0
 )
 ,NonChequeCTE_O_NegAmount AS
 (
 SELECT ROW_NUMBER() over ( Order by regno,Ledgerdate ) as rn,[RecptKey],
  [InstType],[BaseAmount],[PayModeType],[LedgerDate],[InstNo],[Regno],[PostDate]
  ,[ServiceTax],[DVAT]
  ,[Concession],[PaymentStatus],[CenterId],[ddChqCardNo],[ddChqCardDt],[Lumpsump]
  ,[RowId],[concDtlsId],[concDiff]
 FROM @Matching_Table AS BKP2 WHERE  ISNUMERIC(ddChqCardNo)=0 and [InstType]='O' and [BaseAmount]<0
 )
 ,UpdateNonChequeCTE_O_PosAmount AS
 (
   SELECT A.rn,A.[RecptKey],
  A.[InstType],
  --(SELECT B.[BaseAmount] FROM NonChequeCTE_NotO_MinInstNo B WHERE   A.RN=B.RN  )[BaseAmount],
   B.[BaseAmount] ,
  A.[PayModeType],A.[LedgerDate],A.[InstNo],A.[Regno],A.[PostDate],A.[ServiceTax],A.[DVAT]
  ,A.[Concession],A.[PaymentStatus],A.[CenterId],A.[ddChqCardNo],A.[ddChqCardDt]
  ,A.[Lumpsump],A.[RowId],A.[concDtlsId],A.[concDiff]
  FROM NonChequeCTE_O_PosAmount A 
  INNER JOIN NonChequeCTE_NotO_MinInstNo B ON A.RN=B.RN
 )
 ,UpdateNonChequeCTE_O_NegAmount AS
 (
   SELECT A.rn,A.[RecptKey],
  A.[InstType]
  --(SELECT B.[BaseAmount] FROM NonChequeCTE_NotO_MaxInstNo B WHERE   A.RN=B.RN  )[BaseAmount],
  ,B.[BaseAmount]
  ,A.[PayModeType],A.[LedgerDate],A.[InstNo],A.[Regno],A.[PostDate],A.[ServiceTax],A.[DVAT]
  ,A.[Concession],A.[PaymentStatus],A.[CenterId],A.[ddChqCardNo]
  ,A.[ddChqCardDt],A.[Lumpsump],A.[RowId],A.[concDtlsId],A.[concDiff]
  FROM NonChequeCTE_O_NegAmount A 
  INNER JOIN NonChequeCTE_NotO_MaxInstNo B ON   A.RN=B.RN 
 )

 SELECT 1 RN, * FROM @Matching_Table WHERE INSTTYPE<>'O'
 UNION ALL
 SELECT * FROM ChequeCTE1
UNION ALL
   SELECT * FROM UpdateNonChequeCTE_O_PosAmount
UNION ALL
  SELECT * FROM UpdateNonChequeCTE_O_NegAmount

  -- No of rows returned=no of rows in actual table

【讨论】:

我会尝试使用提供的解决方案并还原,我已经分享了想要结果的 sqlfiddle 请查看此链接sqlfiddle.com/#!3/510aa/1 @AmritpalSingh ,您自己检查输出链接,数据与输入相同。我看不出有任何区别。也没有样本数据 Type='O' 并且数量为负数,它不是支票。 我确实查过了,然后转贴。如果您检查第 18、19、20、21 行 ST 是新生成的行。 抱歉回复晚了,很忙。考虑输入样本中的 recptno 0005130 类型 'O' chequeno 559940。现在在输出样本中我看到(-10837.00 和 -4830.00)这个 chequeno。给我们看看输出的计算。这个数量是如何得出的。数字-10837.00是如何计算出来的等等。你会在这里得到很多解决方案。【参考方案2】:

无论您尝试做什么,似乎都涉及在通用编程语言中运行良好的逻辑。

我的建议是使用您最喜欢的编程或脚本语言来连接到您的数据库并检索您的数据,对其进行操作,然后根据需要呈现或存储您的结果。

【讨论】:

以上是关于在 Sql server 中递归分布行的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server 中分布式事务的兼容性

SQL Server 分布式数据库性能测试

在 SQL Server 中创建数据文件 - 数据/表将如何分布?

详解SQL Server分布式查询

SQL Server 分布式事务与本地事务

SQL Server CE 分布