-- 능력이 안되서.. WITH T ( da , id , bu ) AS ( SELECT '10' , '100' ,'390' FROM DUAL UNION ALL SELECT '10' , '100' ,'350' FROM DUAL UNION ALL SELECT '20' , '101' ,'10' FROM DUAL UNION ALL SELECT '20' , '100' ,'20' FROM DUAL UNION ALL SELECT '20' , '102' ,'330' FROM DUAL UNION ALL SELECT '30' , '109' ,'50' FROM DUAL UNION ALL SELECT '30' , '100' ,'50' FROM DUAL ) , T2 AS ( SELECT T.* , ROW_NUMBER() OVER(ORDER BY ROWNUM) RN FROM T ) SELECT A.RN , A.DA , A.ID , A.BU , CASE WHEN DA = 10 THEN 0 WHEN ( SELECT COUNT(*) FROM T2 B WHERE B.RN < A.RN AND B.ID = A.ID ) > 0 THEN 1 ELSE 0 END FROM T2 A ORDER BY A.RN
-- 다시 WITH T ( da , id , bu ) AS ( SELECT '10' , '100' ,'390' FROM DUAL UNION ALL SELECT '10' , '100' ,'350' FROM DUAL UNION ALL SELECT '20' , '101' ,'10' FROM DUAL UNION ALL SELECT '20' , '100' ,'20' FROM DUAL UNION ALL SELECT '20' , '102' ,'330' FROM DUAL UNION ALL SELECT '30' , '109' ,'50' FROM DUAL UNION ALL SELECT '30' , '100' ,'50' FROM DUAL ) , T2 AS ( SELECT T.* , ROW_NUMBER() OVER(ORDER BY ROWNUM) RN FROM T ) SELECT Q.DA , Q.ID , Q.BU , CASE WHEN DA = 10 THEN 0 WHEN GB > 0 THEN 1 ELSE 0 END FLAG_CNT FROM (SELECT A.RN , MAX(A.DA) DA , MAX(A.ID) ID , MAX(A.BU) BU ,COUNT(CASE WHEN A.ID = B.ID THEN 1 END) GB FROM T2 A , T2 B WHERE A.RN > B.RN(+) GROUP BY A.RN ORDER BY A.RN ) Q
WITH T ( da , id , bu ) AS ( SELECT '10' , '100' ,'390' FROM DUAL UNION ALL SELECT '10' , '100' ,'350' FROM DUAL UNION ALL SELECT '20' , '101' ,'10' FROM DUAL UNION ALL SELECT '20' , '100' ,'20' FROM DUAL UNION ALL SELECT '20' , '102' ,'330' FROM DUAL UNION ALL SELECT '30' , '109' ,'50' FROM DUAL UNION ALL SELECT '30' , '100' ,'50' FROM DUAL ) select da, id, bu , case when da = '10' then 0 when row_number() over(partition by id order by da, rownum) = 1 then 0 else 1 end fg from t order by da, rownum