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