-- Schema: C6Mart
-- Stored Procedure: Popola_S133_S157DatiPiramide


  
  
  
  
  
  
  
-- Aggiunta GESTIONE EligoTitoli L192 (FCianfa, 26/11/2014)  
  
CREATE procedure [c6mart].[Popola_S133_S157DatiPiramide]                                                                              
AS                                                                                                                                                
BEGIN                                                                                                                                             
                                                                                                                                           
 SELECT                                                                                                                                    
  PIRAMIDE_MOD.RETE,                                                                                                                
  PIRAMIDE_MOD.COD_FISCALE,                                                                                                    
  PIRAMIDE_MOD.ID_AREA,                                                                                                             
  PIRAMIDE_MOD.PERC_AREA,                                                                                                           
  PIRAMIDE_VERT.CTV_AREA AS CONTROVALORE                                                                                            
 INTO #PIRAMIDE_VERT_TEMP                                                                                                                       
 FROM                                                                                                                                      
  C6Mart.PIRAMIDE_MOD                                                                                                      
 LEFT JOIN (                                                                                                                               
  -- GROUP BY PER ACCORPARE I CTV_AREA SULLE PRE1 E PRE2 IN UNO UNICO CORRISPONDENTE A PRE                                          
     SELECT                                                                                                                                
   RETE,                                                                                                                     
   COD_FISCALE,                                                                                                              
--   LEFT(ID_AREA,3) AS ID_AREA,                                                                                               
  CASE                                                                                                                              
   WHEN ID_AREA = 'SELF'                                                                                                     
   THEN LEFT(ID_AREA,4)                                                                                                      
   ELSE LEFT(ID_AREA,3)                                                                                                      
   END AS ID_AREA,                                                                                                           
   SUM(COALESCE(CTV,0)) AS CTV_AREA                                                                                          
  FROM                                                                                                                              
   C6MART.PATRIMONIO_BF  PIRAMIDE_VERT                                                                              
   GROUP BY                                                                                                                  
   RETE, COD_FISCALE,                                                                                             
   CASE                          
   WHEN ID_ARE
