-- =============================================
-- Author:		<Alessandro Tringali>
-- Create date: <25052010>
-- Description:	< SEZIONE 103>
-- =============================================
--EXEC [C6MartPeriodico].[PL_S103TabellaEmittenti]  'S','RTRRLF23S12Z105V'
CREATE procedure [C6MartPeriodico].[PL_S103TabellaEmittenti_OLD]
	-- Add the parameters for the stored procedure here
	@Rete char(1), 
	@CodiceFiscale varchar(16)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
declare @patr_compl decimal(18,3)
select @patr_compl=sum(ctv_prodotto) from 
c6martperiodico.patrimonio_emittenti 
where  cod_fiscale =@codicefiscale
and rete=@rete
and ctv_prodotto > 0
SELECT 
COD_FISCALE,
RETE,
FLG_RISKCLASS1,
FLG_RISKCLASS2,
FLG_RISKCLASS3,
FLG_RISKCLASS4 ,
INTERMEDIARIO,
ISSUER,
STOCKSCounterValue,
bondsCounterValue,
CONCENTRATION_ISSUER,
CONCENTRATION,
NC
FROM
(
-- PER INTERMEDIARIO 
    SELECT
     COD_FISCALE,
     RETE,
     MAX(FLG_RISKCLASS1) AS FLG_RISKCLASS1,
     MAX(FLG_RISKCLASS2) AS FLG_RISKCLASS2,
     MAX(FLG_RISKCLASS3) AS FLG_RISKCLASS3,
     MAX(FLG_RISKCLASS4) AS FLG_RISKCLASS4 ,
     --V nel caso sia null è BF???
	 isnull(INTERMEDIARIO,'BF') AS INTERMEDIARIO,
     EMITTENTE AS ISSUER,
     SUM(STOCKSCounterValue) AS  STOCKSCounterValue,
     sum(bondsCounterValue) AS bondsCounterValue,
     0 as  CONCENTRATION_ISSUER ,
	 --V normalizzo la percentuale per essere rappresentato sul report
     MAX(CONCENTRATION)* 100 AS CONCENTRATION,
	sum(case when copertura > 0 then 0 else 1 end) as nc
FROM
(
    SELECT     V.COD_FISCALE,
                V.RETE,
                INTERMEDIARIO,
                CREDITRISKCLASS,
                EMITTENTE,
				CASE WHEN CREDITRISKCLASS ='Classe 1' THEN 1 ELSE 0 END AS FLG_RISKCLASS1,
				CASE WHEN CREDITRISKCLASS ='Classe 2' THEN 1 ELSE 0 END AS FLG_RISKCLASS2,
				CASE WHEN CREDITRISKCLASS ='Classe 3' THEN 1 ELSE 0 END AS FLG_RISKCLASS3,
				CASE WHEN CREDITRISKCLASS ='Oltre Classe 3' THEN 1 ELSE 0 END AS FLG_RISKCLASS4,
                SUM(CTV_AZ_OB_EMIT) as CTV_AZ_OB_EMIT,
                SUM(CTV_PRODOTTO) AS CTV_PRODOTTO,
                sum(CTV_AZIONARIO) AS STOCKSCounterValue,
                sum(CTV_OBBLIGAZIONARIO) AS bondsCounterValue,
                0 AS  CONCENTRATION_ISSUER ,
                SUM(CTV_AZ_OB_EMIT_INTER)/SUM(CTV_COMPL_NOCC_NEG)  AS CONCENTRATION,
                sum(copertura) as copertura
 FROM  C6MARTPERIODICO.PATRIMONIO_EMITTENTI V  
 WHERE 
 1=1
   AND V.COD_FISCALE = @CODICEFISCALE
   AND V.RETE = @RETE
   AND (CTV_AZIONARIO >0 OR CTV_OBBLIGAZIONARIO > 0)
GROUP BY
 V.COD_FISCALE,
 V.RETE,
 INTERMEDIARIO,
 EMITTENTE,
 CREDITRISKCLASS
) Y
GROUP BY 
     COD_FISCALE,
     RETE,
     INTERMEDIARIO,
     EMITTENTE
--V devo rappresentare solo le 3 principali me le ordine e nel codice scorro per le prime 3 dopo aver filtrato per intermediario
--ORDER BY CONCENTRATION DESC
UNION 
--COMPLESSIVO
   SELECT
     COD_FISCALE,
     RETE,
     MAX(FLG_RISKCLASS1) AS FLG_RISKCLASS1,
     MAX(FLG_RISKCLASS2) AS FLG_RISKCLASS2,
     MAX(FLG_RISKCLASS3) AS FLG_RISKCLASS3,
     MAX(FLG_RISKCLASS4) AS FLG_RISKCLASS4 ,
	 isnull(INTERMEDIARIO,'BF') AS INTERMEDIARIO,
     EMITTENTE AS ISSUER,
     SUM(STOCKSCounterValue) AS  STOCKSCounterValue,
     SUM(bondsCounterValue) AS bondsCounterValue,
     SUM(isnull(bondsCounterValue,0)+ isnull(STOCKSCounterValue,0))/ max(@patr_compl)  *100  CONCENTRATION_ISSUER ,
     0 AS CONCENTRATION,
	sum(case when copertura > 0 then 0 else 1 end) as nc
FROM
(
SELECT 
COD_FISCALE,
RETE,
'COMPLESSIVO' as INTERMEDIARIO,
CREDITRISKCLASS,
EMITTENTE,
FLG_RISKCLASS1,
FLG_RISKCLASS2,
FLG_RISKCLASS3,
FLG_RISKCLASS4,
SUM(CTV_AZ_OB_EMIT) CTV_AZ_OB_EMIT,
SUM(STOCKSCounterValue) AS STOCKSCounterValue,
SUM(bondsCounterValue) AS bondsCounterValue,
0 AS CONCENTRATION_ISSUER,
SUM(CTV_AZ_OB_EMIT_INTER)/SUM(CTV_COMPLessivo) TOT ,
SUM(COPERTURA) AS COPERTURA
FROM
(
SELECT     V.COD_FISCALE,
                V.RETE,
                'COMPLESSIVO' as INTERMEDIARIO,
                CREDITRISKCLASS,
                EMITTENTE,
				CASE WHEN CREDITRISKCLASS ='Classe 1' THEN 1 ELSE 0 END AS FLG_RISKCLASS1,
				CASE WHEN CREDITRISKCLASS ='Classe 2' THEN 1 ELSE 0 END AS FLG_RISKCLASS2,
				CASE WHEN CREDITRISKCLASS ='Classe 3' THEN 1 ELSE 0 END AS FLG_RISKCLASS3,
				CASE WHEN CREDITRISKCLASS ='Oltre Classe 3' THEN 1 ELSE 0 END AS FLG_RISKCLASS4,
                SUM(CTV_AZ_OB_EMIT) as CTV_AZ_OB_EMIT,
                SUM(CTV_AZIONARIO) AS STOCKSCounterValue,
                sum(CTV_OBBLIGAZIONARIO) AS bondsCounterValue,
                0 AS  CONCENTRATION_ISSUER ,
                SUM(CTV_AZ_OB_EMIT_INTER) CTV_AZ_OB_EMIT_INTER,
                SUM(CTV_COMPLessivo)  AS CTV_COMPLessivo,
                MAX(copertura) as copertura
 FROM  C6MARTPERIODICO.PATRIMONIO_EMITTENTI V  
 WHERE 
 1=1
   AND V.COD_FISCALE = @CODICEFISCALE
   AND V.RETE = @RETE
   AND (CTV_AZIONARIO >0 OR CTV_OBBLIGAZIONARIO > 0)
GROUP BY
 V.COD_FISCALE,
 V.RETE,
 EMITTENTE,
 INTERMEDIARIO,
 CREDITRISKCLASS
) Y
GROUP BY 
COD_FISCALE,
RETE,
INTERMEDIARIO,
CREDITRISKCLASS,
EMITTENTE,
FLG_RISKCLASS1,
FLG_RISKCLASS2,
FLG_RISKCLASS3,
FLG_RISKCLASS4
) L
GROUP BY 
     COD_FISCALE,
     RETE,
     INTERMEDIARIO,
     EMITTENTE
--V devo rappresentare solo le 3 principali me le ordine e nel codice scorro per le prime 3 dopo aver filtrato per intermediario
--ORDER BY CONCENTRATION DESC
) U
ORDER BY CONCENTRATION DESC , CONCENTRATION_ISSUER DESC 
END