SELECT A.TREE_NAME , A.TERRITORY_ID , A.DESCR , A.SYNCID , C.PERSON_ID FROM PS_RSF_TR_SRCH A , PSTREENODE B , PS_RSF_TR_TEAM C WHERE A.TREE_NAME = B.TREE_NAME AND A.TERRITORY_ID = B.TREE_NODE AND B.TREE_NODE IN ( SELECT X.TREE_NODE FROM PSTREENODE X WHERE X.TREE_NODE_NUM >= ( SELECT MIN(N.TREE_NODE_NUM) FROM PSTREENODE N , PS_RSF_TR_TEAM T WHERE N.TREE_NAME = T.TREE_NAME AND N.TREE_NODE = T.TERRITORY_ID AND T.PERSON_ID = C.PERSON_ID AND T.TREE_NAME = B.TREE_NAME AND T.EFFDT = ( SELECT MAX(Y.EFFDT) FROM PS_RSF_TR_TEAM Y WHERE Y.TREE_NAME = T.TREE_NAME AND Y.TERRITORY_ID = T.TERRITORY_ID AND Y.EFFDT <= %CurrentDateIn)) AND X.TREE_NODE_NUM_END <= ( SELECT MAX(N.TREE_NODE_NUM_END) FROM PSTREENODE N , PS_RSF_TR_TEAM T WHERE N.TREE_NAME = T.TREE_NAME AND N.TREE_NODE = T.TERRITORY_ID AND T.PERSON_ID = C.PERSON_ID AND T.TREE_NAME = B.TREE_NAME AND T.EFFDT = ( SELECT MAX(Y.EFFDT) FROM PS_RSF_TR_TEAM Y WHERE Y.TREE_NAME = T.TREE_NAME AND Y.TERRITORY_ID = T.TERRITORY_ID AND Y.EFFDT <= %CurrentDateIn))) AND B.TREE_NAME = C.TREE_NAME AND C.EFFDT = ( SELECT MAX(Y.EFFDT) FROM PS_RSF_TR_TEAM Y WHERE Y.TREE_NAME = C.TREE_NAME AND Y.TERRITORY_ID = C.TERRITORY_ID AND Y.EFFDT <= %CurrentDateIn)
|