;With ResetImageGenFlag_CTE AS
(
SELECT
ua.ind_no,
a.individual_id,
a.id,a.ff_id,
a.dateEnd,
ua.imagegen_dt,
DATEDIFF(SS,(dateEnd),ua.imagegen_dt) as TimeElapse,
ua.imagegen_flag,
CASE WHEN DATEDIFF(SS,a.dateEnd,ua.imagegen_dt) <=120 then 0 else ua.imagegen_flag end as [Flag],
ROW_NUMBER() Over(Partition By individual_id Order By [dateEnd] DESC) Rnk
FROM uaimage ua
INNER JOIN
( SELECT ff.individual_id,frr.*
FROM uas.fileread_reads frr
INNER JOIN uas.fil