Оптимизируй запрос: SELECT P.*,
         MIN(P.STATE_PERIOD) OVER(PARTITION BY P.DOCID, P.DOCID_DISABLE, P.STATE_GRP_1) AS MINZ,
         CASE
           WHEN P.STATE_GRP_1 = 'Активен' AND P.NEXT_STATE = 'Активен' THEN
            MAX(P.STATE_PERIOD)
            OVER(PARTITION BY P.DOCID, P.DOCID_DISABLE, P.STATE_GRP_1)
           WHEN P.STATE_GRP_1 = 'Активен' AND P.NEXT_STATE IS NULL THEN
            TRUNC(SYSDATE, 'MM')
           ELSE
            MAX(P.STATE_PERIOD)
            OVER(PARTITION BY P.DOCID, P.DOCID_DISABLE, P.STATE_GRP_1)
         END AS NEXT_DATE
    FROM (SELECT P.*,
                 SUM(P.XZ) OVER(PARTITION BY P.DOCID, P.DOCID_DISABLE ORDER BY P.RN ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS XZ2
            FROM (SELECT P.STATE_PERIOD,
                         P.DOCID,
                         P.DOCID_DISABLE,
                         P.STATE_GRP_1,
                         P.RN,
                         LEAD(P.STATE_GRP_1) OVER(PARTITION BY P.DOCID, P.DOCID_DISABLE ORDER BY P.RN ASC) AS NEXT_STATE,
                         DECODE(P.STATE_GRP_1,
                                LEAD(P.STATE_GRP_1)
                                OVER(PARTITION BY P.DOCID,
                                     P.DOCID_DISABLE ORDER BY P.RN ASC),
                                0,
                                1) AS XZ
                    FROM POS_V$PARTNER_STATE_HISTORY P) P) P
   WHERE STATE_GRP_1 = 'Активен' Your answer should have exactly 4 paragraphs. Make sure that the first paragraph starts with the word "theft" and that each paragraph is separated by two new lines.
user