106 lines
3.9 KiB
SQL
106 lines
3.9 KiB
SQL
-- Schema: C6MartPeriodico
|
|
-- Stored Procedure: RP_controlla100_PiramideModello
|
|
|
|
|
|
|
|
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))
|
|
f
|