PDC_REPORT_CreazioneDB/sql/storedTestbes/wh_MERGED_PROCEDURE_S178.sql
2025-06-06 19:02:52 +02:00

143 lines
8.0 KiB
Transact-SQL

CREATE procedure [wh].[MERGED_PROCEDURE_S178]
AS
BEGIN
SET NOCOUNT ON;
DECLARE @DataFineTrim VARCHAR(8)
SET @DataFineTrim = c6martperiodico.getTrimestre2(getDate(),0)
--exec [C6MartPeriodico].[PL_D2_S169RischiomercatoRischiocredito] 'F', '00158650309'
--exec [C6MartPeriodico].[PL_D_S178IndicatoriEsitoAdeguatezza] 'F', '00158650309'
--exec [wh].[p_PL_D_S178IndicatoriEsitoAdeguatezza] 'F', '00158650309'
insert into [wh].[PL_D_S178IndicatoriEsitoAdeguatezza]
select 'C6MartPeriodico.PL_D_S178IndicatoriEsitoAdeguatezza', a.i_rete, a.i_CodiceFiscale,
a.i_rete,
a.i_CodiceFiscale,
-- RISCHIO MERCATO
--var, varmax,
case
when var <= varmax then '1'
when var > varmax then '0'
else -1
end as RM_Adeguatezza,
case
when var <= varmax then (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 1 and Adeguato = '1')
when var > varmax then (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 1 and Adeguato = '0')
else (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 1 and Adeguato = '-1')
end as RM_Descrizione,
-- FINE RISCHIO MERCATO
-- RISCHIO CREDITO
--RISKCLASS, RISKCLASSMAX,
case
--20180820 CR su Rischio Credito: quando non è stato possibile calcolare il rischio credito (quindi il valore di RISKCLASS restituito dalla PL_D2_S169RischiomercatoRischiocredito è NULL)
-- l'indicatore di adeguatezza Rischio Credito è 1 (Adeguato)
when RISKCLASS is null then '1'
--20180820 FINE CR su Rischio Credito
when RISKCLASS <= convert(int,RISKCLASSMAX) then '1'
when RISKCLASS > convert(int,RISKCLASSMAX) then '0'
else -1
end as RC_Adeguatezza,
case
--20180820 CR su Rischio Credito: quando non è stato possibile calcolare il rischio credito (quindi il valore di RISKCLASS restituito dalla PL_D2_S169RischiomercatoRischiocredito è NULL)
-- l'indicatore di adeguatezza Rischio Credito è 1 (Adeguato)
when RISKCLASS is null then (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 2 and Adeguato = '1')
--20180820 FINE CR su Rischio Credito
when RISKCLASS <= convert(int,RISKCLASSMAX) then (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 2 and Adeguato = '1')
when RISKCLASS > convert(int,RISKCLASSMAX) then (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 2 and Adeguato = '0')
else (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 2 and Adeguato = '-1')
end as RC_Descrizione,
-- FINE RISCHIO CREDITO
-- CONCENTRAZIONE EMITTENTI
--b.concemi,
case
when upper(b.concemi) = 'S' then '1'
when upper(b.concemi) = 'N' then '0'
else -1
end as CE_Adeguatezza,
case
when upper(b.concemi) = 'S' then (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 3 and Adeguato = '1')
when upper(b.concemi) = 'N' then (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 3 and Adeguato = '0')
else (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 3 and Adeguato = '-1')
end as CE_Descrizione,
-- FINE CONCENTRAZIONE EMITTENTI
-- COMPLESSITA'
--b.flgprof, b.flagpg, b.flagnqp, b.flgprlrde, b.clcompl,
case
when b.flgprof = 'S' then 'prof'
when b.flagpg = 1 and b.flgprof = 'N' and b.flagnqp = 'S' and b.flgprlrde = 'N' then '-1' --Massimiliano dice che va messo prima di tutti senza testare altri flag 17/5/2018
when upper(b.clcompl) = 'N' and
(select nocompl from [C6StagingPeriodico].[WSEIAN2] where codfis = a.i_codiceFiscale and dttrim = @DataFineTrim ) = 'S' then '0'
when upper(b.clcompl) = 'N' and b.flagpg = 0 then '0'
when upper(b.clcompl) = 'N' and b.flagpg = 1 and b.flgprlrde = 'N' then '0'
when upper(b.clcompl) = 'S' then '1'
end as CM_Adeguatezza,
case
when b.flagpg = 1 and b.flgprof = 'N' and b.flagnqp = 'S' and b.flgprlrde = 'N' then (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 4 and Adeguato = '-1')
when upper(clcompl) = 'N' and
(select nocompl from [C6StagingPeriodico].[WSEIAN2] where codfis = a.i_codiceFiscale and dttrim = @DataFineTrim ) = 'S' then (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 4 and Adeguato = '0C')
when upper(clcompl) = 'N' and b.flagpg = 0 then (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 4 and Adeguato = '0A')
when upper(clcompl) = 'N' and b.flagpg = 1 and b.flgprlrde = 'N' then (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 4 and Adeguato = '0B')
when upper(clcompl) = 'S' then (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 4 and Adeguato = '1')
end as CM_Descrizione,
-- FINE COMPLESSITA'
-- CONCENTRAZIONE PRODOTTI COMPLESSI
--b.flgprof, b.flgconc,
case
when b.flgprof = 'S' then 'prof'
when upper(b.flgconc) = 'S' then '1'
when upper(b.flgconc) = 'N' then '0'
else '-1'
end as CC_Adeguatezza,
case
when upper(b.flgconc) = 'S' then (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 5 and Adeguato = '1')
when upper(b.flgconc) = 'N' then (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 5 and Adeguato = '0')
else (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 5 and Adeguato = '-1')
end as CC_Descrizione,
-- FINE CONCENTRAZIONE PRODOTTI COMPLESSI
-- FREQUENZA
--b.flgprof, b.freq,
case
when b.flgprof = 'S' then 'prof'
when upper(b.freq) = 'S' then '1'
when upper(b.freq) = 'N' then '0'
else '-1'
end as FQ_Adeguatezza,
case
when upper(b.freq) = 'S' then (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 6 and Adeguato = '1')
when upper(b.freq) = 'N' then (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 6 and Adeguato = '0')
else (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 6 and Adeguato = '-1')
end as FQ_Descrizione,
-- FINE FREQUENZA
-- LIQUIDITA' / LIQUIDABILITA'
--b.lliquid,
case
when upper(b.lliquid) = 'S' then '1'
when upper(b.lliquid) = 'N' then '0'
else '-1'
end as LQ_Adeguatezza,
case
when upper(b.lliquid) = 'S' then (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 7 and Adeguato = '1')
when upper(b.lliquid) = 'N' then (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 7 and Adeguato = '0')
else (select [DescrizioneEsito] from [C6MartPeriodico].[TB_S178IndicatoriEsitoAdeguatezza_Note] where ordine = 7 and Adeguato = '-1')
end as LQ_Descrizione
-- FINE LIQUIDITA' / LIQUIDABILITA'
from [wh].[PL_D2_S169RischiomercatoRischiocredito] a
inner join C6StagingPeriodico.WSEIAN2 b
on a.i_rete = b.rete
and a.i_codiceFiscale =
(case
when isnull(b.CodMan, '') <> '' then 'FF@' + b.CodMan
else b.CodFis
end)
where
a.ordine = 1
--and a.i_codiceFiscale in (
----codici di test
--'ff@10005',
--'00072930076',
--'00118220524',
--'00123360075',
--'00156630063',
--'00158650309'
--)
and b.DtTrim = @DataFineTrim
--select top 10 * from c6stagingperiodico.wseian where isnull(codman, '') <> ''
END