update t set t.materialAmount=temp.amount from TousseItem t inner join (select sum(mi.count) amount ,ti.id from TousseItem ti inner join TousseDefinition td  on td.id = ti.tousseDefinitionId inner join MaterialInstance mi on mi.tousse_id = td.id group by ti.id) temp on temp.id=t.id;
update r set r.materialAmount=temp.amount from RecyclingItem r inner join (select sum(mi.count) amount ,ri.id from RecyclingItem ri inner join TousseDefinition td  on td.id = ri.tousseDefinitionId inner join MaterialInstance mi on mi.tousse_id = td.id group by ri.id) temp on temp.id=r.id;
update c set c.materialAmount=temp.amount from ClassifiedItem c inner join (select sum(mi.count) amount ,ci.id from ClassifiedItem ci inner join TousseDefinition td  on td.id = ci.toussedefinition_id inner join MaterialInstance mi on mi.tousse_id = td.id group by ci.id) temp on temp.id=c.id;
update p set p.materialAmount=temp.amount from PackingRecord p inner join (select sum(mi.count) amount ,pr.id from PackingRecord pr inner join TousseDefinition td  on td.id = pr.tousseDefinitionId inner join MaterialInstance mi on mi.tousse_id = td.id group by pr.id) temp on temp.id=p.id;
update t set t.materialAmount=temp.amount from TousseInstance t inner join (select sum(mi.count) amount ,ti.id from TousseInstance ti inner join TousseDefinition td  on td.id = ti.toussedefinition_id inner join MaterialInstance mi on mi.tousse_id = td.id group by ti.id) temp on temp.id=t.id;
update i set i.materialAmount=temp.amount from InvoiceItem i inner join (select sum(mi.count) amount ,ii.id from InvoiceItem ii inner join TousseDefinition td  on td.id = ii.tousseDefinitionId inner join MaterialInstance mi on mi.tousse_id = td.id group by ii.id) temp on temp.id=i.id;
update r set r.materialAmountAtThatTime=temp.amount from ReturnGoodsItem r inner join (select sum(mi.count) amount ,ri.id from ReturnGoodsItem ri inner join TousseDefinition td  on td.id = ri.toussedefinition_id inner join MaterialInstance mi on mi.tousse_id = td.id group by ri.id) temp on temp.id=r.id;
update t set t.materialAmount=temp.amount from TousseDefinition t inner join (select sum(mi.count) amount ,td.id from TousseDefinition td inner join MaterialInstance mi on mi.tousse_id = td.id group by td.id) temp on temp.id=t.id;