请帮助解决以下问题,我有一组记录,我需要为给定的ID和Case_Type循环,并将记录标记为Valid或ERROR。
由于它是一个审核表,因此事件必须按顺序排列,例如打开/挂起/完成或打开/完成
因此Rec_no 1和4是以下样本数据中的ERROR。 Rec_no 5(对于ID 9)带有最新有效状态(已完成')。
以下是样本记录 :
Rec_num ID Case_Type Status
1 9 1 Pend -- ERROR (Out of Order)
2 9 1 open
3 8 1 open
4 9 1 open -- ERROR (Duplicate)
5 9 1 done
6 9 1 open
7 8 1 done

我需要以下输出:
Lag and Lead functions are not available in DB2 z/OS.
This is what i have till now. I guess I need something like
max(case when previous_stat = 'open' and (Status = 'Pend' or Status = 'done' then 1 else 0) as Flag over(partition by ID,Case_Type order by Rec_num).
WITH TST AS
(SELECT 1 AS Rec_Num, 9 AS ID, 1 AS Case_Type, 'Pend' AS Status FROM
SYSIBM.SYSDUMMY1 UNION ALL
SELECT 2 AS Rec_Num, 9 AS ID, 1 AS Case_Type, 'open' AS Status FROM
SYSIBM.SYSDUMMY1 UNION ALL
SELECT 3 AS Rec_Num , 8 AS ID,2 AS Case_Type, 'open' AS Status FROM
SYSIBM.SYSDUMMY1 UNION ALL
SELECT 4 AS Rec_Num, 9 AS ID, 1 AS Case_Type, 'open' AS Status FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 5 AS Rec_Num, 9 AS ID, 1 AS Case_Type, 'done' AS Status FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 6 AS Rec_Num, 9 AS ID, 1 AS Case_Type, 'open' AS Status FROM
SYSIBM.SYSDUMMY1
union all
SELECT 7 AS Rec_Num, 8 AS ID, 1 AS Case_Type, 'done' AS Status FROM
SYSIBM.SYSDUMMY1)
SELECT ID,Case_Type, Status, MAX(Status) OVER(PARTITION BY ID,Case_Type ORDER BY
Rec_Num ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) AS PREVIOUS_STAT from TST

Rec_num ID Case_Type Status Flag
1 9 1 Pend ERROR
2 9 1 open Valid
3 8 1 open Valid
4 9 1 open ERROR
5 9 1 done Valid
6 9 1 open ERROR
7 8 1 done Valid