PDC_REPORT_CreazioneDB/sql/storedCollaudo/C6Mart_PL_D2_S170RischioDiversificazione.sql
2025-06-06 19:02:52 +02:00

144 lines
3.8 KiB
Transact-SQL

-- Schema: C6Mart
-- Stored Procedure: PL_D2_S170RischioDiversificazione
-- [C6Mart].[PL_D_S170RischioDiversificazione] 'S','02148400126'
-- Aggiunta GESTIONE EligoTitoli L192 (FCianfa, 01/12/2014)
CREATE procedure [C6Mart].[PL_D2_S170RischioDiversificazione]--'F','05228020268'
-- Add the parameters for the stored procedure here
@Rete char(1),
@CodiceFiscale varchar(16)
AS
BEGIN
declare @contaCCSelf as int
select @contaCCSelf = count(*)
from C6MART.patrimonio_Bf
where tipo_prodotto = 'CC' or isin in('EURO00000009', 'EURO10000007' ) and cod_fiscale = @codiceFiscale and rete = @rete
declare @contaPOS as int
select @contaPOS = count(*)
from C6MART.patrimonio_Bf
where cod_fiscale = @codiceFiscale and rete = @rete
declare @ctvbf as decimal(12,2)
SELECT @ctvbf = isnull(sum(ctv),0) FROM c6mart.patrimonio_bf
where cod_fiscale = @CodiceFiscale
and rete= @Rete
and CTV > 0
declare @ctvterzi as decimal (12,2)
SELECT @ctvterzi = isnull(sum(ctv),0) FROM c6mart.patrimonio_terzi
where cod_fiscale = @CodiceFiscale
and rete= @Rete
and CTV > 0
-- Complessità
declare @maxComplessita as int
select @maxComplessita = max(compl_base) from [C6Mart].[TB_S180ProdottiComplessitaAlta]
where
cod_fiscale like
CASE when @CodiceFiscale like '%FF@%'THEN '%%'
ELSE @CodiceFiscale
END
and codman =
CASE when @CodiceFiscale like '%FF@%' THEN substring(@CodiceFiscale, 4,len(@CodiceFiscale)-3)
ELSE ''
END
and rete= @Rete
-- 20180703 bisogna escludere l'eventuale compl_base = 10, che non verrebbe decodificata
and compl_base <> 10
--
SELECT
K.COD_FISCALE,
K.RETE,
CASE
when cod_Aggreg = 'COMPLESSIVO|BF' then @ctvbf
when cod_Aggreg = 'COMPLESSIVO|TERZI' then @ctvterzi
when cod_Aggreg = 'COMPLESSIVO' then @ctvbf + @ctvterzi
else 0
END AS CTV_AGGREG,
K.CREDITRISK,
CASE
WHEN (@contaPOS - @contaCCSelf = 0) THEN 0
ELSE VAR_PERC_PTF
END AS VAR,
CASE WHEN ISNULL(COPERTURA,0.00) = 0.00 AND @contaPOS - @contaCCSelf <> 0 THEN 'n.c.' ELSE NULL END
as varString,
CASE
WHEN (ISNULL(copertura,100) < 100) AND @contaPOS - @contaCCSelf <> 0
--THEN 'Grado di copertura: ' + REPLACE(CAST(CAST(COPERTURA AS DECIMAL (5,2)) AS VARCHAR),'.',',') + '%'
THEN REPLACE(CAST(CAST(COPERTURA AS DECIMAL (5,2)) AS VARCHAR),'.',',') --+ '%'
ELSE null
END AS coperturaString,
beneficio as diversificazione,
CASE
WHEN (@contaPOS - @contaCCSelf = 0) THEN 100
ELSE COPERTURA
END AS COPERTURA,
case
when cod_Aggreg = 'COMPLESSIVO' then 'Patrimonio Complessivo'
when cod_Aggreg = 'COMPLESSIVO|BF' then 'Patrimonio ' + (case when k.rete ='F' then 'Fideuram' when k.rete = 'S' then 'Sanpaolo Invest' else 'IW Private Inv.' end)
when cod_Aggreg = 'COMPLESSIVO|TERZI' then 'Patrimonio altri Istituti'
ELSE ''
END AS PATRIMONIO,
case
when b.profilo is null then -1
else b.profilo
end as profiloCode,
b.max_var as var_profilo,
------------------------ MIFID2 - 20180711
case @maxComplessita
when 0 then 'Non complesso'
when 1 then 'Bassa'
when 2 then 'Medio bassa'
when 3 then 'Medio alta'
when 4 then 'Alta'
else 'n.c.'
end
as complessita,
------------------------- FINE MIFID2 - 20180711
--V l'ordine serve per la corretta visualizzazione del report: Patrimonio casa, patrimonio terzi, patrimonio complessivo
--V Monitoraggio prende soltanto la prima riga, diagnosi anche nel caso non ci siano i terzi.
case
when cod_Aggreg = 'COMPLESSIVO' then 3
when cod_Aggreg = 'COMPLESSIVO|BF' then 1
when cod_Aggreg = 'COMPLESSIVO|TERZI' then 2
ELSE ''
END AS Ordine
FROM C6MART.RISCHIO_AGGREGATO K
left outer JOIN C6MART.mifid a
ON k.cod_fiscale = a.cod_fiscale and
k.rete = a.rete
left outer join C6MART.CODIFICA_RISCHIO b
ON A.PROFILO_ASS=B.PROFILO
WHERE
--A.PROFILO_ASS=B.PROFILO
--AND K.RETE=A.RETE
--AND K.COD_FISCALE= A.COD