CREATE OR REPLACE VIEW V_PROFILE_RT_CODE_FOR_ENRO AS /** Show rows from enquiry_roles along with start_date of the next instance of the same rt_code and enquiry. */ SELECT prty_id ,rt_code ,start_date ,end_date ,next_start_date ,enqu_id FROM (SELECT enro.prty_id ,enro.rt_code ,enro.start_date ,enro.end_date ,enqu_id ,lead(start_date ,1) over(PARTITION BY rt_code, enqu_id ORDER BY start_date) AS next_start_date FROM enquiry_roles enro);