设置查询时遇到问题,该查询将用于从其他行的现有行中获取信息。
现有表格数据
在上图中,RecptNo 20100001182有三行。
在Recptno 20100001182中,我有三张支票 分别为Instno 5,6,7检查编号000005,000006,000007
使用Recptno 20100001970
返回recptno 20100001182的检查但是recptno 20100001970没有捕获instno所以需要在recpt中分发匹配的check no。
期望输出
现在在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
答案 0 :(得分:0)
我只是在尝试我理解的东西。所以不要DOWNVOTE。
最重要的是,我没有看到任何欲望输出。如果你提供,那么它将有助于快速理解。
我的理解,
所以如果以上是真的那么只有3个条件
i)具有chequeNo的InstType'O'ii)具有负BaseAmount的InstType'O' iii)具有正BaseAmount的InstType'O'
所以基于小提琴样本数据。它可以在没有光标的情况下完成,并且可以进一步优化,只要事情更清晰。
;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
答案 1 :(得分:-1)
无论你想做什么,似乎都涉及在通用编程语言中运行良好的逻辑。
我的建议是使用您喜欢的编程或脚本语言连接到您的数据库并检索您的数据,对其进行操作,然后根据需要显示或存储您的结果。