您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。