create table exe_offsets(exe_code char(3) not null, months_offset integer not null) insert into exe_offsets values('001', 6) insert into exe_offsets values('002', 10) insert into exe_offsets values('003', 12) select distinct mfe.prm_year,exe_eid,exe_per,'ΔΕΝ ΠΡΟΣΗΛΘΕ' from mfe,exe,fo, exe_offsets where -- begin additions exe_offsets.exe_code = exe.exe_code and isnull(fo.fo_ana, '99991231') > dateadd(month, exe_offsets.months_offset, convert(datetime, mfe.prm_year + '/09/30')) and --end additions exe.div_code=mfe.div_code and exe.tm_code=mfe.tm_code and exe.prm_year=mfe.prm_year and exe.exe_code=mfe.exe_code and fo.fo_code='018198600006' and mfe.prm_year>=fo_ee and mfe.prm_year+exe_eid not in (select distinct mfe.prm_year+exe_eid from mfe,exe where exe.div_code=mfe.div_code and exe.tm_code=mfe.tm_code and exe.prm_year=mfe.prm_year and exe.exe_code=mfe.exe_code and fo_code='018198600006')