gpt4 book ai didi

sql - DATEADD 未返回某些行的正确日期

转载 作者:行者123 更新时间:2023-12-04 20:56:19 26 4
gpt4 key购买 nike

该函数应在当前日期之后返回一个新日期并添加 num_of_flows。 num_of_flows 是一个整数值

ALTER  function [dbo].[M20_22_GetLoanMaturityDate_FIN_10]
(
@AcctAcid varchar(50),
@ScheduleNum varchar(50),
@bank_id varchar(6) = '03'
)
returns varchar(50)
as
BEGIN
Declare @lMaturityDate varchar(50)
begin
select @lMaturityDate =
case Finacle_Lrs.LR_FREQ_TYPE
when 'W' then dateadd(day, (Finacle_Lrs.NUM_OF_FLOWS - 1) * 7,Finacle_Lrs.FLOW_START_DATE)
when 'F' then dateadd(day, (Finacle_Lrs.NUM_OF_FLOWS - 1) * 14,Finacle_Lrs.FLOW_START_DATE)
when 'M' then (dateadd(month, Finacle_Lrs.NUM_OF_FLOWS - 1, Finacle_Lrs.FLOW_START_DATE))
when 'Q' then (dateadd(month, (Finacle_Lrs.NUM_OF_FLOWS - 1) * 3, Finacle_Lrs.FLOW_START_DATE))
when 'H' then dateadd(month, (Finacle_Lrs.NUM_OF_FLOWS - 1) * 6, Finacle_Lrs.FLOW_START_DATE)
when 'Y' then dateadd(month, (Finacle_Lrs.NUM_OF_FLOWS - 1) * 12, Finacle_Lrs.FLOW_START_DATE)
end
from [TEST_EIMDW_Archive].[ARCHOWN].[FINCL10_LRS] Finacle_Lrs
where Finacle_Lrs.acid = @AcctAcid
and Finacle_Lrs.shdl_num = @ScheduleNum
and Finacle_Lrs.FLOW_ID in ('PRDEM','EIDEM')
AND Finacle_Lrs.ACTIVE_FLAG = 'Y' AND Finacle_Lrs.DELETE_FLAG= 'N'
and Finacle_Lrs.bank_id = @bank_id
and Finacle_Lrs.FLOW_START_DATE = ( select max(a.FLOW_START_DATE)
from [TEST_EIMDW_Archive].[ARCHOWN].[FINCL10_LRS] a
where a.ACID = Finacle_Lrs.ACID
and a.SHDL_NUM = Finacle_Lrs.SHDL_NUM
and a.FLOW_ID in ('PRDEM', 'EIDEM') AND a.ACTIVE_FLAG = 'Y' AND a.DELETE_FLAG='N' and a.bank_id = @bank_id
);
end
return isnull(@lMaturityDate,null);

END

在做了一些检查之后,我注意到 altest 某天在比较 oracle 数据库上的原始代码时低估了 19629 行。请看oracle版本的代码:

FUNCTION GetLoanMaturityDate(AcctAcid VARCHAR2, ScheduleNum VARCHAR2) RETURN DATE IS
lMaturityDate DATE;
BEGIN
begin
select decode(lrs.LR_FREQ_TYPE, 'W', lrs.FLOW_START_DATE + (lrs.NUM_OF_FLOWS - 1) * 7,
'F', lrs.FLOW_START_DATE + (lrs.NUM_OF_FLOWS - 1) * 14,
'M', add_months(lrs.FLOW_START_DATE, lrs.NUM_OF_FLOWS - 1) ,
'Q', add_months(lrs.FLOW_START_DATE, (lrs.NUM_OF_FLOWS - 1) * 3),
'H', add_months(lrs.FLOW_START_DATE, (lrs.NUM_OF_FLOWS - 1) * 6),
'Y', add_months(lrs.FLOW_START_DATE, (lrs.NUM_OF_FLOWS - 1) * 12)
)
into lMaturityDate
from lrs
where lrs.acid = AcctAcid
and lrs.shdl_num = ScheduleNum
and lrs.FLOW_ID in ('PRDEM','EIDEM')
and lrs.FLOW_START_DATE = ( select max(a.FLOW_START_DATE)
from lrs a
where a.ACID = lrs.ACID
and a.SHDL_NUM = lrs.SHDL_NUM
and a.FLOW_ID in ('PRDEM', 'EIDEM')
) ;
exception
when no_data_found then
lMaturityDate:=NULL;
end;

return lMaturityDate;
END GetLoanMaturityDate;

这也是我为了查看不正确的行而运行的比较查询:

SELECT eim.acid as eim_acid,it.[ACID] as it_acid,
eim.[MATURITY_DATE] as eim_maturity_date,it.[MATURITY_DATE] as it_maturity_date ,
eim.[DAYS_TO_MATURITY] as eim_DAYS_TO_MATURITY,it.[DAYS_TO_MATURITY] as it_DAYS_TO_MATURITY,(it.[DAYS_TO_MATURITY]-eim.[DAYS_TO_MATURITY]) as diff
FROM [TEST_EIMDW_BOJ_REPORTS].[BOJOWN].[loans_maturity_profile] eim
inner join [dbo].[IT_LOANS_MATURITY_PROFILE] it on eim.acid= it.acid
where eim.MATURITY_DATE != it.MATURITY_DATE

最后,下面是查询结果,显示了正确结果集与我当前尝试在 sql server 上修复的结果集的比较:

enter image description here

最佳答案

补充一下 Gordon 所说的,Oracle 的 ADD_MONTHS 函数的确切行为是,如果源日期是该月的最后一天,那么返回日期也是该月的最后一天结果月份(无论源日期月份的长度如何)。

SQL Server没有类似的功能。

哪种实现是正确的,或者它是否重要,将取决于您的具体情况。

关于sql - DATEADD 未返回某些行的正确日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56245968/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com