-- Schema: C6MartPeriodico
-- Stored Procedure: Popola_S80_S129_EvoluzioneCTVPatrimonio_percentuale_Fase3











-- =============================================
-- Author:		Marian Zaki
-- Create date: 29/11/2010
-- Description:	Checks for data validation 
-- =============================================
CREATE procedure [C6MartPeriodico].[Popola_S80_S129_EvoluzioneCTVPatrimonio_percentuale_Fase3]
-- Add the parameters for the stored procedure here


AS
begin
DECLARE @DataCongelamento DATETIME  
  
 --DECLARE @DettMovPrec money  
 --DECLARE @DettMovCorr money  
 --DECLARE @AppoDettMovPrec varchar(50)  
 --DECLARE @AppoDettMovCorr varchar(50)  
 --DECLARE @NumAsterischiPrec int  
 --DECLARE @NumAsterischiCorr int  

  
 --SET @NumAsterischiPrec = 0  
 --SET @NumAsterischiCorr = 0  
  
 SET @DataCongelamento = C6MartPeriodico.getDataCongelamento()  
  
drop table if exists c6martperiodico.TB_Popola_S80_S129_EvoluzioneCTVPatrimonio_percentuale_Fase3



SELECT   
 rete,  
 cod_fiscale,  
   3 AS Fase,  
   CASE  
    WHEN @DataCongelamento IS NULL THEN GETDATE()  
    ELSE @DataCongelamento  
   END AS DataFase,  
   --Get the sum Operazioni in corso   
   --sum((isnull(IMPORTO,0))) as Partita_viaggiante,  
   0 as Partita_viaggiante,  
   sum(case Upper(id_area) when 'EXT' then ctv else 0 end ) as CTV_EXT,  
   sum(case Upper(id_area) when 'INV' then ctv else 0 end ) as CTV_INV,  
   sum(case when Upper(id_area) like 'PRE%' then ctv else 0 end ) as CTV_PRE,  
   --sum(case Upper(id_area) when 'PRE' then ctv else 0 end ) as CTV_PRE,  
   sum(case Upper(id_area) when 'RIS' then ctv else 0 end ) as CTV_RIS,     
   sum(case Upper(id_area) when 'LIQ' then ctv else 0 end ) as CTV_LIQ, 
   
   -- aggiunta del 19-10-2020 per il calcolo delle percentuali delle varie aree della piramide
 --  	case
	--	when (sum(case Upper(id_area) when 'EXT' then ctv else 0 end )+ sum(case Upper(id_area) when 'INV' then ctv else 0 end )+ sum(case when Upper(id_area) like 'PRE%' then ctv else 0 end ) +  
	--		sum(case Upper(id_area) when 'RIS' then ctv else 0 end )+ sum(case Upper(id_area) when 'LIQ' then ctv else 0 end )) = 0
	--		then 0.0 
	--		else 
	--			sum(case Upper(id_area) when 'EXT' then ctv else 0 end ) / (sum(case Upper(id_area) when 'EXT' then ctv else 0 end )+ sum(case Upper(id_area) when 'INV' then ctv else 0 end )+ sum(case when Upper(id_area) like 'PRE%' then ctv else 0 end ) +  
	--			sum(case Upper(id_area) when 'RIS' then ctv else 0 end )+ sum(case Upper(id_area) when 'LIQ' then ctv else 0 end )) * 100 
	--end as PERC_EXT,
	--case 
	--	when (sum(case Upper(id_area) when 'EXT' then ctv else 0 end )+ sum(case Upper(id_area) when 'INV' then ctv else 0 end )+ sum(case when Upper(id_area) like 'PRE%' then ctv else 0 end ) +  
	--		sum(case Upper(id_area) when 'RIS' then ctv else 0 end )+ sum(case Upper(id_area) when 'LIQ' then ctv else 0 end )) = 0
	--	then 0.0
	--	else
	--		sum(case Upper(id_area) when 'INV' then ctv else 0 end ) / (sum(case Upper(id_area) when 'EXT' then ctv else 0 end )+ sum(case Upper(id_area) when 'INV' then ctv else 0 end )+ sum(case when Upper(id_area) like 'PRE%' then ctv else 0 end ) +  
	--		sum(case Upper(id_area) when 'RIS' then ctv else 0 end )+ sum(case Upper(id_area) when 'LIQ' then ctv else 0 end )) * 100
	--end as PERC_INV,
	--case
	--	when (sum(case Upper(id_area) when 'EXT' then ctv else 0 end )+ sum(case Upper(id_area) when 'INV' then ctv else 0 end )+ sum(case when Upper(id_area) like 'PRE%' then ctv else 0 end ) +  
	--		  sum(case Upper(id_area) when 'RIS' then ctv else 0 end )+ sum(case Upper(id_area) when 'LIQ' then ctv else 0 end )) = 0
	--	then
	--		0.0
	--	else
	--		sum(case when Upper(id_area) like 'PRE%' then ctv else 0 end ) / (sum(case Upper(id_area) when 'EXT' then ctv else 0 end )+ sum(case Upper(id_area) when 'INV' then ctv else 0 end )+ sum(case when Upper(id_area) like 'PRE%' then ctv else 0 end ) +  
	--		sum(case Upper(id_area) when 'RIS' then ctv else 0 end )+ sum(case Upper(id_area) when 'LIQ' then ctv el
