В этом случае вы можете использовать функцию Lag.
Пожалуйста, найдите ниже запрос, который дает ожидаемый результат.
SELECT CUSTOMERICN,
CUSTOMERID,
PROCDATE,
PROCDATE - PREVIOUSDATE DIFFERENCE_DAYS,
CASE
WHEN PROCDATE - PREVIOUSDATE > 365 THEN
PROCDATE
WHEN PREVIOUSDATE IS NULL THEN
PROCDATE
ELSE
NULL
END RESULTDATE
FROM (WITH TEMP AS(
SELECT '10001213' CUSTOMERICN ,'282212' CUSTOMERID,TO_DATE('01/08/2003','MM/DD/YYYY') PROCDATE FROM DUAL UNION
SELECT '10001213','282212',TO_DATE('07/26/2007','MM/DD/YYYY') FROM DUAL UNION
SELECT '10001213','282212',TO_DATE('02/04/2002','MM/DD/YYYY') FROM DUAL UNION
SELECT '10001215','244563',TO_DATE('04/11/2012','MM/DD/YYYY') FROM DUAL UNION
SELECT '10001219','279379',TO_DATE('11/26/2007','MM/DD/YYYY') FROM DUAL UNION
SELECT '10001219','279379',TO_DATE('06/16/2008','MM/DD/YYYY') FROM DUAL UNION
SELECT '10001219','279379',TO_DATE('04/28/2011','MM/DD/YYYY') FROM DUAL UNION
SELECT '10001219','279379',TO_DATE('01/14/2013','MM/DD/YYYY') FROM DUAL UNION
SELECT '10006912','256568',TO_DATE('10/12/2007','MM/DD/YYYY') FROM DUAL UNION
SELECT '10069018','257703',TO_DATE('09/10/2001','MM/DD/YYYY') FROM DUAL UNION
SELECT '10069018','257703',TO_DATE('01/29/2008','MM/DD/YYYY') FROM DUAL UNION
SELECT '10069018','257703',TO_DATE('06/12/2008','MM/DD/YYYY') FROM DUAL UNION
SELECT '10069018','257703',TO_DATE('09/24/2008','MM/DD/YYYY') FROM DUAL UNION
SELECT '10069018','257703',TO_DATE('10/24/2008','MM/DD/YYYY') FROM DUAL UNION
SELECT '10069018','257703',TO_DATE('11/17/2008','MM/DD/YYYY') FROM DUAL UNION
SELECT '10006403','255643',TO_DATE('06/29/2001','MM/DD/YYYY') FROM DUAL UNION
SELECT '10006403','255643',TO_DATE('09/09/2001','MM/DD/YYYY') FROM DUAL
)
SELECT CUSTOMERICN,
CUSTOMERID,
PROCDATE,
LAG(PROCDATE, 1) OVER(PARTITION BY CUSTOMERID ORDER BY PROCDATE) PREVIOUSDATE
FROM TEMP
ORDER BY CUSTOMERID, PROCDATE)
ORDER BY 1, 2, 3
Здесь я для каждого клиента, я получаюпредыдущая дата с помощью функции отставания.Затем вы можете разместить оставшуюся логику (количество дней меньше 365 и т. Д.)
И результат будет таким, как показано ниже.
CUSTOMERICN CUSTOMERID PROCDATE DIFFERENCE_DAYS RESULTDATE
10001213 282212 2/4/2002 2/4/2002
10001213 282212 1/8/2003 338
10001213 282212 7/26/2007 1660 7/26/2007
10001215 244563 4/11/2012 4/11/2012
10001219 279379 11/26/2007 11/26/2007
10001219 279379 6/16/2008 203
10001219 279379 4/28/2011 1046 4/28/2011
10001219 279379 1/14/2013 627 1/14/2013
10006403 255643 6/29/2001 6/29/2001
10006403 255643 9/9/2001 72
10006912 256568 10/12/2007 10/12/2007
10069018 257703 9/10/2001 9/10/2001
10069018 257703 1/29/2008 2332 1/29/2008
10069018 257703 6/12/2008 135
10069018 257703 9/24/2008 104
10069018 257703 10/24/2008 30
10069018 257703 11/17/2008 24