Select sum(金额) FROM (Select 客户编码,sum(金额) as 金额 FROM (Select gl_accass.ccus_id as 客户编码,(case when gl_accass.cbegind_c<>N'贷' then gl_accass.mb else -gl_accass.mb end ) as 金额 FROM code INNER JOIN gl_accass ON code.ccode = gl_accass.ccode WHERE gl_accass.iperiod=1 and gl_accass.ccode like N'1133%' and gl_accass.ccus_id is not null union Select GL_accvouch.ccus_id as 客户编码,(GL_accvouch.md-GL_accvouch.mc) as 金额 FROM code INNER JOIN gl_accvouch ON code.ccode = gl_accvouch.ccode WHERE GL_accvouch.iflag is null and iperiod<>0 and iperiod<13 and GL_accvouch.ccode like N'1133%' and dbill_date<=N'2008-12-31' and GL_accvouch.ccus_id is not null) group by 客户编码) where 金额<0
Select sum(金额) FROM (Select 客户编码,sum(金额) as 金额 FROM (Select gl_accass.ccus_id as 客户编码,(case when gl_accass.cbegind_c <>N'贷' then gl_accass.mb else -gl_accass.mb end ) as 金额 FROM code INNER JOIN gl_accass ON code.ccode = gl_accass.ccode WHERE gl_accass.iperiod=1 and gl_accass.ccode like N'1133%' and gl_accass.ccus_id is not null union Select GL_accvouch.ccus_id as 客户编码,(GL_accvouch.md-GL_accvouch.mc) as 金额 FROM code INNER JOIN gl_accvouch ON code.ccode = gl_accvouch.ccode WHERE GL_accvouch.iflag is null and iperiod <>0 and iperiod <13 and GL_accvouch.ccode like N'1133%' and dbill_date <=N'2008-12-31' and GL_accvouch.ccus_id is not null) a group by 客户编码) b where 金额 <0