CREATE procedure [C6MartPeriodico].[RP_controlla100_PiramideModello] AS BEGIN drop table if exists dbo.piramidemodello_da_agg_manualmente drop table if exists #conta drop table if exists #conta_b drop table if exists #conta_agg select a.rete,a.cod_fiscale, fase, sum(a.perc) as somma into #conta from (select rete,cod_fiscale,perc_ext as perc, fase from c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc where fase in (1,2,3) union all select rete,cod_fiscale,perc_inv as perc, fase from c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc where fase in (1,2,3) union all select rete,cod_fiscale,perc_pre as perc, fase from c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc where fase in (1,2,3) union all select rete,cod_fiscale,perc_liq as perc, fase from c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc where fase in (1,2,3) union all select rete,cod_fiscale,perc_ris as perc, fase from c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc where fase in (1,2,3) ) a --where cod_fiscale not in ('DNTNDA68P53D458Y','FLSGRL63S17L872V') group by rete,cod_fiscale, fase having sum(a.perc) <> '100.00' order by cod_fiscale, rete, fase --select * from #conta select ChiaveCliente, c.* into #conta_b from #conta c inner join consuni.consulenzaunica.dbo.Cliente cli on cli.codFis = c.cod_fiscale UNION ALL select chiavecliente, c.* from #conta c inner join consuni.consulenzaunica.dbo.Cliente cli on cli.Piva = c.cod_fiscale UNION ALL select chiavecliente, c.* from #conta c inner join consuni.consulenzaunica.dbo.Cliente cli on c.cod_fiscale like 'FF@%' and cli.codman = substring(c.cod_fiscale, 4, len(c.cod_fiscale)-3) --select * from #conta_b -- 155194 -- 181778 select clipb.chiaveclientepb, clipb.chiavePB, clipb.flagpbprimario, pb.codage agente, pb.Rete pb_Rete, cb.* into #conta_agg from #conta_b cb inner join consuni.consulenzaunica.dbo.ClientePB clipb ON clipb.ChiaveCliente = cb.ChiaveCliente INNER JOIN consuni.consulenzaunica.dbo.PromotoreBancario pb ON pb.chiavePB = clipb.chiavePB -- Per eliminare i criteri poco performanti nella creazione di #conta_agg (and clipb.flagpbprimario is null pb.Rete = cd.Rete ) Delete #conta_agg where rete != pb_Rete or flagpbprimario is not null --select * from #conta_agg order by chiaveclientepb -- 155182 ---- OLD -- select chiaveclientepb, agente, c.* -- into #conta_agg -- from #conta c -- inner join consuni.consulenzaunica.dbo.vinfocliente v -- on c.rete+c.cod_fiscale = v.rete+v.codfis or -- c.rete+c.cod_fiscale = v.rete+v.piva or -- c.rete+c.cod_fiscale = v.rete+'FF@'+v.codman -- where flagpbprimario is null -- -- select * from #conta_agg order by 4 select sum(percentualeprogetto) as somma, a.area, cod_fiscale, rete, p.chiaveclientepb, con.fase into dbo.piramidemodello_da_agg_manualmente from consuni.ConsulenzaUnica.dbo.ProgettiPiramideModello pir inner join consuni.ConsulenzaUnica.dbo.areamodello a on a.chiaveareamod = pir.chiaveareamod inner join consuni.ConsulenzaUnica.dbo.PiramideModello p on p.chiavepiramidemod = a.chiavepiramidemod inner join #conta_agg con on con.chiaveclientepb = p.chiaveclientepb where a.area in ('Ext', 'Liq', 'Inv', 'Ris', 'Pre') group by a.area, cod_fiscale, rete, p.chiaveclientepb, con.fase order by 3, 2 --select * from dbo.piramidemodello_da_agg_manualmente -- 775865 --select * from c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc where Cod_Fiscale = 'DNTNDA68P53D458Y' update ctv set perc_ext = cast(isnull(somma, '0.00') as decimal(5,2)) from piramidemodello_da_agg_manualmente agg inner join c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc ctv on agg.cod_fiscale+agg.rete = ctv.cod_fiscale+ctv.rete where agg.area = 'Ext' and ctv.Fase = agg.Fase update ctv set perc_inv = cast(isnull(somma, '0.00') as decimal(5,2)) from piramidemodello_da_agg_manualmente agg inner join c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc ctv on agg.cod_fiscale+agg.rete = ctv.cod_fiscale+ctv.rete where area = 'Inv' and ctv.Fase = agg.Fase update ctv set perc_Liq = cast(isnull(somma, '0.00') as decimal(5,2)) from piramidemodello_da_agg_manualmente agg inner join c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc ctv on agg.cod_fiscale+agg.rete = ctv.cod_fiscale+ctv.rete where area = 'Liq' and ctv.Fase = agg.Fase update ctv set perc_Ris= cast(isnull(somma, '0.00') as decimal(5,2)) from piramidemodello_da_agg_manualmente agg inner join c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc ctv on agg.cod_fiscale+agg.rete = ctv.cod_fiscale+ctv.rete where area = 'Ris' and ctv.Fase = agg.Fase update ctv set perc_pre= cast(isnull(somma, '0.00') as decimal(5,2)) from piramidemodello_da_agg_manualmente agg inner join c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc ctv on agg.cod_fiscale+agg.rete = ctv.cod_fiscale+ctv.rete where area = 'Pre' and ctv.Fase = agg.Fase -- -- FASE 2: --drop table if exists dbo.piramidemodello_da_agg_manualmente --drop table if exists #conta2 --drop table if exists #conta_agg2 --select a.rete,a.cod_fiscale, sum(a.perc) as somma --into #conta2 --from --(select rete,cod_fiscale,perc_ext as perc from c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc where fase = 2 --union all --select rete,cod_fiscale,perc_inv as perc from c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc where fase = 2 --union all --select rete,cod_fiscale,perc_pre as perc from c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc where fase = 2 --union all --select rete,cod_fiscale,perc_liq as perc from c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc where fase = 2 --union all --select rete,cod_fiscale,perc_ris as perc from c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc where fase = 2 --) a ----where cod_fiscale not in ('DNTNDA68P53D458Y','FLSGRL63S17L872V') --group by rete,cod_fiscale --having sum(a.perc) <> '100.00' --order by cod_fiscale ----select * from #conta --select chiaveclientepb, agente, c.* --into #conta_agg2 --from #conta2 c --inner join consuni.consulenzaunica.dbo.vinfocliente v --on c.rete+c.cod_fiscale = v.rete+v.codfis or --c.rete+c.cod_fiscale = v.rete+v.piva or --c.rete+c.cod_fiscale = v.rete+'FF@'+v.codman --where flagpbprimario is null ----select * from #conta_agg order by 4 -- select sum(percentualeprogetto) as somma, a.area, cod_fiscale, rete, p.chiaveclientepb -- into dbo.piramidemodello_da_agg_manualmente -- from consuni.ConsulenzaUnica.dbo.ProgettiPiramideModello pir -- inner join consuni.ConsulenzaUnica.dbo.areamodello a -- on a.chiaveareamod = pir.chiaveareamod -- inner join consuni.ConsulenzaUnica.dbo.PiramideModello p -- on p.chiavepiramidemod = a.chiavepiramidemod -- inner join #conta_agg2 con -- on con.chiaveclientepb = p.chiaveclientepb -- where a.area in ('Ext', 'Liq', 'Inv', 'Ris', 'Pre') -- group by a.area, cod_fiscale, rete, p.chiaveclientepb -- order by 3, 2 -- --select * from dbo.piramidemodello_da_agg_manualmente -- --select * from c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc where Cod_Fiscale = 'DNTNDA68P53D458Y' -- update ctv -- set perc_ext = cast(isnull(somma, '0.00') as decimal(5,2)) -- from piramidemodello_da_agg_manualmente agg -- inner join c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc ctv -- on agg.cod_fiscale+agg.rete = ctv.cod_fiscale+ctv.rete -- where agg.area = 'Ext' -- and ctv.Fase = 2 -- update ctv -- set perc_inv = cast(isnull(somma, '0.00') as decimal(5,2)) -- from piramidemodello_da_agg_manualmente agg -- inner join c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc ctv -- on agg.cod_fiscale+agg.rete = ctv.cod_fiscale+ctv.rete -- where area = 'Inv' -- and ctv.Fase = 2 -- update ctv -- set perc_Liq = cast(isnull(somma, '0.00') as decimal(5,2)) -- from piramidemodello_da_agg_manualmente agg -- inner join c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc ctv -- on agg.cod_fiscale+agg.rete = ctv.cod_fiscale+ctv.rete -- where area = 'Liq' -- and ctv.Fase = 2 -- update ctv -- set perc_Ris= cast(isnull(somma, '0.00') as decimal(5,2)) -- from piramidemodello_da_agg_manualmente agg -- inner join c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc ctv -- on agg.cod_fiscale+agg.rete = ctv.cod_fiscale+ctv.rete -- where area = 'Ris' -- and ctv.Fase = 2 -- update ctv -- set perc_pre= cast(isnull(somma, '0.00') as decimal(5,2)) -- from piramidemodello_da_agg_manualmente agg -- inner join c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc ctv -- on agg.cod_fiscale+agg.rete = ctv.cod_fiscale+ctv.rete -- where area = 'Pre' -- and ctv.Fase = 2 -- -- FASE 3: --drop table if exists dbo.piramidemodello_da_agg_manualmente --drop table if exists #conta3 --drop table if exists #conta_agg3 --select a.rete,a.cod_fiscale, sum(a.perc) as somma --into #conta3 --from --(select rete,cod_fiscale,perc_ext as perc from c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc where fase = 3 --union all --select rete,cod_fiscale,perc_inv as perc from c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc where fase = 3 --union all --select rete,cod_fiscale,perc_pre as perc from c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc where fase = 3 --union all --select rete,cod_fiscale,perc_liq as perc from c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc where fase = 3 --union all --select rete,cod_fiscale,perc_ris as perc from c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc where fase = 3 --) a ----where cod_fiscale not in ('DNTNDA68P53D458Y','FLSGRL63S17L872V') --group by rete,cod_fiscale --having sum(a.perc) <> '100.00' --order by cod_fiscale ----select * from #conta --select chiaveclientepb, agente, c.* --into #conta_agg3 --from #conta3 c --inner join consuni.consulenzaunica.dbo.vinfocliente v --on c.rete+c.cod_fiscale = v.rete+v.codfis or --c.rete+c.cod_fiscale = v.rete+v.piva or --c.rete+c.cod_fiscale = v.rete+'FF@'+v.codman --where flagpbprimario is null ----select * from #conta_agg order by 4 -- select sum(percentualeprogetto) as somma, a.area, cod_fiscale, rete, p.chiaveclientepb -- into dbo.piramidemodello_da_agg_manualmente -- from consuni.ConsulenzaUnica.dbo.ProgettiPiramideModello pir -- inner join consuni.ConsulenzaUnica.dbo.areamodello a -- on a.chiaveareamod = pir.chiaveareamod -- inner join consuni.ConsulenzaUnica.dbo.PiramideModello p -- on p.chiavepiramidemod = a.chiavepiramidemod -- inner join #conta_agg3 con -- on con.chiaveclientepb = p.chiaveclientepb -- where a.area in ('Ext', 'Liq', 'Inv', 'Ris', 'Pre') -- group by a.area, cod_fiscale, rete, p.chiaveclientepb -- order by 3, 2 -- --select * from dbo.piramidemodello_da_agg_manualmente -- --select * from c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc where Cod_Fiscale = 'DNTNDA68P53D458Y' -- update ctv -- set perc_ext = cast(isnull(somma, '0.00') as decimal(5,2)) -- from piramidemodello_da_agg_manualmente agg -- inner join c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc ctv -- on agg.cod_fiscale+agg.rete = ctv.cod_fiscale+ctv.rete -- where agg.area = 'Ext' -- and ctv.Fase = 3 -- update ctv -- set perc_inv = cast(isnull(somma, '0.00') as decimal(5,2)) -- from piramidemodello_da_agg_manualmente agg -- inner join c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc ctv -- on agg.cod_fiscale+agg.rete = ctv.cod_fiscale+ctv.rete -- where area = 'Inv' -- and ctv.Fase = 3 -- update ctv -- set perc_Liq = cast(isnull(somma, '0.00') as decimal(5,2)) -- from piramidemodello_da_agg_manualmente agg -- inner join c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc ctv -- on agg.cod_fiscale+agg.rete = ctv.cod_fiscale+ctv.rete -- where area = 'Liq' -- and ctv.Fase = 3 -- update ctv -- set perc_Ris= cast(isnull(somma, '0.00') as decimal(5,2)) -- from piramidemodello_da_agg_manualmente agg -- inner join c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc ctv -- on agg.cod_fiscale+agg.rete = ctv.cod_fiscale+ctv.rete -- where area = 'Ris' -- and ctv.Fase = 3 -- update ctv -- set perc_pre= cast(isnull(somma, '0.00') as decimal(5,2)) -- from piramidemodello_da_agg_manualmente agg -- inner join c6MartPeriodico.TB_S80_S129_EvoluzioneCTVPatrimonio_perc ctv -- on agg.cod_fiscale+agg.rete = ctv.cod_fiscale+ctv.rete -- where area = 'Pre' -- and ctv.Fase = 3 ----select * from C6MartPeriodico.motivazioni_blacklist end