CREATE OR REPLACE FORCE VIEW v_lead_times
AS
SELECT enty_code
      ,mety_code
      ,mesc_code
      ,svcpt_code
      ,nvl(from_qmax
          ,0) AS from_qmax
      ,nvl(to_qmax
          ,9999999999999999) AS to_qmax
          ,days
  FROM (SELECT enty_code
              ,mety_code
              ,mesc_code
              ,svcpt_code
              ,lag(qmax) over(PARTITION BY enty_code, mety_code, svcpt_code ORDER BY qmax) AS from_qmax
              ,qmax AS to_qmax
              ,days
          FROM (SELECT enty_code
                      ,mety_code
                      ,mesc_code
                      ,svcpt_code
                      ,qmax
                      ,days
                  FROM lead_times       leti
                      ,meter_size_codes mesc
                 WHERE mesc.code(+) = leti.mesc_code))
/
