在Sql server中递归分布行

在Sql server中递归分布行,sql,sql-server,sql-server-2012,sql-update,recursive-query,Sql,Sql Server,Sql Server 2012,Sql Update,Recursive Query,我无法设置一个查询,该查询将用于从其他行的现有行中获取信息 现有表数据 在上图中,RecptNo 20100001182有三行 在第20100001182号收据中,我有三张支票 支票编号000005000006000007,分别用于票据编号5、6、7 使用recptno 20100001720返回recptno 20100001182支票 但收据编号20100001970不包含instno,因此需要在两个收据中分发匹配的支票编号 期望输出 当弹跳或负输入完成时,由 负数量和类型=O。 如果是支票

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

现有表数据

在上图中,RecptNo 20100001182有三行

在第20100001182号收据中,我有三张支票 支票编号000005000006000007,分别用于票据编号5、6、7

使用recptno 20100001720返回recptno 20100001182支票

但收据编号20100001970不包含instno,因此需要在两个收据中分发匹配的支票编号

期望输出

当弹跳或负输入完成时,由 负数量和类型=O。 如果是支票或没有支票号码,则应根据该日期之前存在且具有相同chqno的现有+ve金额行分配-ve金额 应先拾取最高Instno的数量,然后再进行下降 在总金额分配之前,请先登记。 现在,在收据20100001971中,收到的金额用于掩盖退回的支票

收到的20100001971号收据中的金额需要适当分配。现在,当Type=O,amount为正时,我们应该首先选择Minimum Instno,然后按升序选择rest,直到总数被正确划分

预期结果应如下所示。

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

    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

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


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

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


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

我只是在尝试我理解的内容。所以不要投反对票

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

我的理解是

您需要更新BaseAmount,其中instype='O'和您提到的所有其他条件。 不需要在InstType为“O”的情况下更新BaseMount。它只是有助于更新上面的第1点。InstType如“I”、“R”、“A”在本需求中具有相同的用途。 根据您的解释,此要求只有两种类型的InstType,即“O”和“其他”。 因此,如果上述条件成立,则只有3个条件

i带有chequeNo的InstType“O”型仪器ii带有负基金额的InstType“O”型仪器 iii具有正基金额的“O”型仪表

所以,基于fiddle样本数据,它可以在没有光标的情况下完成,并且可以进一步优化,前提是事情更加清楚


我只是在尝试我所理解的,所以不要投反对票

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

我的理解是

您需要更新BaseAmount,其中instype='O'和您提到的所有其他条件。 不需要在InstType为“O”的情况下更新BaseMount。它只是有助于更新上面的第1点。InstType如“I”、“R”、“A”在本需求中具有相同的用途。 根据您的解释,此要求只有两种类型的InstType,即“O”和“其他”。 因此,如果上述条件成立,则只有3个条件

i带有chequeNo的InstType“O”型仪器ii带有负基金额的InstType“O”型仪器 iii具有正基金额的“O”型仪表

所以,基于fiddle样本数据,它可以在没有光标的情况下完成,并且可以进一步优化,前提是事情更加清楚


很难理解你需要什么。到目前为止你有什么疑问?你觉得recptno 20100001970怎么样?@HoneyBadger实际上我所代表的案例是针对一个用户的。因此,显示的交易属于一个用户,交易主要有三种类型:I、O-ve金额、O+veamount@MaciejLos我已经用我所拥有的更新了我的问题tried@AmritpalSingh,仍然无法理解您的逻辑;是否要计算运行总和?在RcptNo=20100001971的情况下,你想要什么?通过什么?@MaciejLos我们可以聊天吗?我将解释难以理解你需要什么。到目前为止你有什么疑问?你觉得recptno 20100001970怎么样?@HoneyBadger实际上我所代表的案例是针对一个用户的。因此,显示的交易属于一个用户,交易主要有三种类型:I、O-ve金额、O+veamount@MaciejLos我已经用我所拥有的更新了我的问题tried@AmritpalSingh,仍然无法理解您的逻辑;是否要计算运行总和?如果RcptNo=20100001971,您使用了什么?我们可以聊天吗?@MaciejLos我将解释我将尝试使用提供的解决方案并恢复,我已共享了des的SQLFIDLE
ired结果请检查此链接@AmritpalSingh,您自己检查输出链接,数据与输入相同。我看不出任何差异。此外,没有样本数据Type='O',金额为负数,不是支票。我实际检查了,然后重新提交。如果您勾选第18、19、20、21行是新生成的行。很抱歉回复晚了,在输入样本中考虑RePTNO 0005130型‘O’CHECHENO 559940。现在在输出样本中,我看到了这个CHECHENO的-10837.00和-48 30.00。请告诉我们输出量的计算。这个量是如何得出的。图10837.00是如何计算的。等等。您将在这里得到大量的解决方案。我将尝试使用所提供的解决方案。回复,我已共享了所需结果的SqlFIDLE请检查此链接@Amritpalingh,您自己检查输出链接,数据与输入相同。我看不出任何差异。此外,没有样本数据类型='O',金额为负数,也不是支票。我实际检查并重新发布。如果您勾选第18、19、20、21行是新生成的行。很抱歉回复晚了,在输入样本中考虑RePTNO 0005130型‘O’CHECHENO 559940。现在在输出样本中,我看到了这个CHQENEO的-10837.00和-48 30.00。请告诉我们输出量的计算。这个量是如何得出的。图10837.00是如何计算的,等等。您将在这里得到大量的解决方案。
;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