select null id, cast(a.acc_set as varchar(50)) key1, cast(a.years as varchar(50)) key2,cast(a.months as varchar(50)) key3, a.acc_name key4, '' key5, b.distNo bh, b.distName mc, b.distNo, b.ztid, CAST(12 as numeric) lvalue,cast(isnull(zzny,0)-isnull(fsny,0) as numeric) as rvalue,cast(isnull(zzny,0)-isnull(fsny,0)-12 as numeric) as zvalue, '' writer, '' writeDate from ( select acc_set,acc_name,years,months,(years*12+months) fsny from hd_bjAccountnew..balance where exists( select * from (select acc_set,acc_name,MAX(years*12+months) fsny from hd_bjAccountnew..balance where acc_code like '154%' and (ISNULL(debit_month,0)<>0 or ISNULL(credit_month,0)<>0) and exists(select acc_set,acc_code from hd_bjAccountnew..acc_code where lastflag=1 and acc_code like '154%' and exists(select years,acc_set from hd_bjAccountnew..acc_set where acc_set.years=acc_code.years and acc_set.acc_set=acc_code.acc_set) and acc_code.acc_set=balance.acc_set and acc_code.acc_code=balance.acc_code)and (ISNULL(debit_month,0)<>0 or ISNULL(credit_month,0)<>0) group by acc_set,acc_name )a where a.acc_set=balance.acc_set and a.acc_name=balance.acc_name and a.fsny=(balance.years*12+balance.months) ) )a join (select acc_set,years,months,years*12+months zzny from hd_bjAccountnew..acc_set)c on a.acc_set=c.acc_set join zcjy_hd..zt b on a.acc_set=b.BS_ztNo where zzny-fsny>12