SELECT DISTINCT A.OPRID , C.LM_ADHC_ID , C.LM_ADHC_TYPE FROM PS_LM_OPRID_LE_VW A , PS_LM_ADHC_LE_TBL B , PS_LM_ADHC_TBL C WHERE A.LM_LE_ID = B.LM_LE_ID AND B.LM_ADHC_ID = C.LM_ADHC_ID AND ((C.EFFDT = ( SELECT MAX(C1.EFFDT) FROM PS_LM_ADHC_TBL C1 WHERE C1.LM_ADHC_ID = C.LM_ADHC_ID AND C1.EFFDT <= %CurrentDateIn)) OR (C.EFFDT > %CurrentDateIn AND NOT EXISTS ( SELECT 'X' FROM PS_LM_ADHC_TBL C2 WHERE C2.LM_ADHC_ID = C.LM_ADHC_ID AND C2.EFFDT <= %CurrentDateIn) AND C.EFFDT = ( SELECT MIN(C1.EFFDT) FROM PS_LM_ADHC_TBL C1 WHERE C1.LM_ADHC_ID = C.LM_ADHC_ID)))
|