CREATE procedure [level2martperiodico].[DM_PatrimonioBFAggregato]  
AS  
truncate table [level2martperiodico].[PatrimonioBFAggregato]  
insert into [level2martperiodico].[PatrimonioBFAggregato]  
SELECT     PATRIMONIO_BF.RETE, PATRIMONIO_BF.COD_FISCALE,   
   SUM(CTV) as CTV,  
   sum(case Upper(id_area) when 'NA' then ctv else 0 end ) as PatrimonioNonAssociato,  
   sum(case Upper(id_area) when 'CC' then ctv else 0 end ) as ContoCorrente,  
   sum(case Upper(id_area) when 'CC' then 0 else ctv end ) as PatrimonioSenzaCC,  
   --MAX((isnull(PART_VIAGG.PARTVIA_INV,0))) as PartiteViaggiantiInv,  
   sum((isnull([C6MartPeriodico].PATRIMONIO_BF.PARTVIA_DISINV,0))) as PartiteViaggiantiDisinv  
FROM         [C6MartPeriodico].PATRIMONIO_BF  
GROUP BY PATRIMONIO_BF.RETE, PATRIMONIO_BF.COD_FISCALE