select max(d.DOC_ID)/1000 as max_doc
from docs d, ORGANIZATION o
where mod(d.DOC_ID, 1000)=o.KM_SNODE_ID