CREATE OR REPLACE VIEW V_AICO AS SELECT id ,adit_code ,selling_price ,cost_price ,delivery_cost ,regi_code ,enty_code ,mety_code ,mesc_code ,valid_from AS effective_from ,least(nvl(valid_to ,TO_DATE('2099' ,'yyyy')) ,nvl(next_valid_from ,TO_DATE('2099' ,'yyyy'))) AS effective_to FROM (SELECT id ,adit_code ,selling_price ,cost_price ,delivery_cost ,regi_code ,enty_code ,mety_code ,mesc_code ,valid_from ,valid_to ,lead(valid_from) over(PARTITION BY adit_code, regi_code, enty_code, mety_code, mesc_code ORDER BY valid_from) AS next_valid_from FROM costs cost WHERE cost_type = 'AICO');