select o.orgname,t3.sumb,b.period,b.balamt,b.busdate,b.createdate
from new_jsjg_ie.dbo.iest_balance b,new_jsjg_ie.dbo.iecm_org o,
(select orgname+max(period) nm,sum(b.balamt) sumb
from new_jsjg_ie.dbo.iest_balance b,new_jsjg_ie.dbo.iecm_org o
where b.orgid=o.id and b.balamt<>0 group by o.orgname)t3
where b.orgid=o.id
and o.orgname+b.period=nm order by period desc
1.9号更新,增加所属分公司
select (select orgname from new_jsjg_ie.dbo.iecm_org where id=c.locationindeprid) 所属分公司, c.contractno 合同编码,o.orgname 项目名称, convert(varchar(100),c.planstartdate,23)合同开始时间, convert(varchar(100),c.planenddate,23)合同结束时间, convert(varchar(100),convert(decimal(18,2),c.contractcost))+'万' 合同额, convert(varchar(100),convert(decimal(18,2),t3.sumb/10000))+'万' 开累业主计量, b.period 最新月,convert(varchar(100),convert(decimal(18,2),b.balamt/10000))+'万' 最新月业主计量, convert(varchar(100),b.busdate,23) 建账日期 from new_jsjg_ie.dbo.iest_balance b,new_jsjg_ie.dbo.iecm_org o, new_jsjg_ie.dbo.iect_contract c , (select orgname+max(period) nm,sum(b.balamt) sumb from new_jsjg_ie.dbo.iest_balance b,new_jsjg_ie.dbo.iecm_org o where b.orgid=o.id and b.balamt<>0 group by o.orgname)t3 where b.orgid=o.id and o.id=c.orgid and o.orgname+b.period=nm and c.contracttype='P1' order by period desc