您好,登录后才能下订单哦!
IF EXISTS(SELECT FROM SYS.TABLES WHERE NAME='SPD') DROP TABLE SPD;
WITH CTE1 AS
(
SELECT
IIF(PD_DueDT>=ISNULL(PD_DebitSuccDT,'9999-12-31'),0,1) AS FLAG/逾期FLAG/,
ROW_NUMBER() OVER(PARTITION BY PD_EFH_SN ORDER BY PD_Termth) AS ID/期次序号/,
MAX(PD_Termth) OVER (PARTITION BY PD_EFH_SN)AS CurrID/当前最大期次序号*/,
FROM PD WHERE PD_DueDT<=GETDATE() AND PD_Termth>0
),
CTE2 AS
(
SELECT
SUM(FLAG) OVER(PARTITION BY PD_EFH_SN ORDER BY PD_Termth ROWS UNBOUNDED PRECEDING) AS AccuFlag/累计逾期期次/,
FROM CTE1
),
CTE3 AS
(
SELECT
ID-AccuFlag AS FlagGrp/逾期分组/,
FROM CTE2
),
CTE4 AS
(
SELECT
SUM(FLAG) OVER (PARTITION BY PD_EFH_SN,FlagGrp) AS CountFlag/分组计数/,
FROM CTE3 WHERE FLAG=1
),
CTE5 AS
(
SELECT
MAX(CountFlag) OVER (PARTITION BY PD_EFH_SN) AS MaxContFlag/最大连续/,
MAX(AccuFlag) OVER (PARTITION BY PD_EFH_SN) AS TotFlag/历史累计/,
IIF(ID=PD_TotTerm,CountFlag,0) AS CurrContFlag/当前累计/,
ROW_NUMBER() OVER (PARTITION BY PD_EFH_SN ORDER BY PD_Termth DESC) AS CurrTermth,
FROM CTE4
)
SELECT DISTINCT PD_EFH_SN,MaxContFlag,TotFlag,CurrContFlag,'XFY' AS DataSource INTO SPD FROM CTE5 WHERE CurrTermth=1
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。