-- Schema: C6Mart
-- Stored Procedure: PL_S103TabellaEmittenti



-- =============================================
-- Author:		<Alessandro Tringali>
-- Create date: <25052010>
-- Description:	< SEZIONE 103>
-- =============================================
--EXEC [c6mart].[PL_S103TabellaEmittenti]  'F','BRGGRL48H52G096C'

--select distinct creditriskclass from c6mart.patrimonio_emittenti

CREATE procedure [C6Mart].[PL_S103TabellaEmittenti]
	-- 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)
----Issuer
--CreditRisk
select @patr_compl=sum(ctv_prodotto) from 
c6mart.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,
FLG_RISKCLASS5,
FLG_RISKCLASS6,
FLG_RISKCLASS7,
FLG_RISKCLASS8,
FLG_RISKCLASS9,
FLG_RISKCLASS10,
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,
	MAX(FLG_RISKCLASS5) AS FLG_RISKCLASS5,
	MAX(FLG_RISKCLASS6) AS FLG_RISKCLASS6,
	MAX(FLG_RISKCLASS7) AS FLG_RISKCLASS7,
	MAX(FLG_RISKCLASS8) AS FLG_RISKCLASS8,
	MAX(FLG_RISKCLASS9) AS FLG_RISKCLASS9,
	MAX(FLG_RISKCLASS10) AS FLG_RISKCLASS10,
     --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,
     case when sum(copertura) > 0 then 0 else 1 end as nc
FROM
(
    SELECT     V.COD_FISCALE,
                V.RETE,
                INTERMEDIARIO,
                CREDITRISKCLASS,
                EMITTENTE,
				CASE WHEN CREDITRISKCLASS ='Rischio 1/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS1,
				CASE WHEN CREDITRISKCLASS ='Rischio 2/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS2,
				CASE WHEN CREDITRISKCLASS ='Rischio 3/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS3,
				CASE WHEN CREDITRISKCLASS ='Rischio 4/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS4,
				CASE WHEN CREDITRISKCLASS ='Rischio 5/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS5,
				CASE WHEN CREDITRISKCLASS ='Rischio 6/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS6,
				CASE WHEN CREDITRISKCLASS ='Rischio 7/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS7,
				CASE WHEN CREDITRISKCLASS ='Rischio 8/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS8,
				CASE WHEN CREDITRISKCLASS ='Rischio 9/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS9,
				CASE WHEN CREDITRISKCLASS ='Rischio 10/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS10,
                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  c6mart.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

 UNION 
   SELECT
     COD_FISCALE,
     RETE,
     MAX(FLG_RISKCLASS1) AS FLG_RISKCLASS1,
     MAX(FLG_RISKCLASS2) AS FLG_RISKCLASS2,
     MAX(FLG_RISKCLASS3) AS FL
