Description
Properties
| Name | Value |
|---|---|
| ANSI Nulls ON | True |
| Quoted Identifier ON | True |
| Encrypted | False |
| Execute As | |
| Assembly |
Parameters
1| Name | Data Type | Length | Description |
|---|---|---|---|
| @p_Cuit | varchar | 20 |
SQL Script
Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
CREATE OR ALTER PROCEDURE dbo.obtener_garantias_bancos_mas_singarantias
@p_Cuit varchar(20)
AS
BEGIN
SET NOCOUNT ON;
declare @p_5anios date = getdate() - (5*365)
declare @p_2anios date = getdate() - (2*365)
declare @p_3meses date = getdate() - (90)
declare @periodo_maximo date,@periodo_maximo_cuit date,@fecha_formateada varchar(10),@periodo_comparar varchar(6);
select @periodo_maximo = (select max(fecha_informacion) from fuentes_procesadas where visible = '1' and fuente ='cendeu')
select @periodo_maximo_cuit = convert(date,isnull(max(cendeu.fec_inf),'200112')+'01') from cendeu where iden = @p_cuit AND vigente = 1
if (@periodo_maximo_cuit > @periodo_maximo)
begin
set @periodo_maximo = @periodo_maximo_cuit
end
if @periodo_maximo is not null
begin
select convert(int,sum(cgarprefa+garprefa)) con_garantias_a,convert(int,sum(cgarprefb+garprefb)) as con_garantias_b,
convert(int,sum(sincgarpre+SINgarpref)) sin_garantias,(convert(int,sum(prestamos+particip+garotor+o_cptos))) total,convert(date,fec_inf+'01') as periodo from cendeu
where convert(date,fec_inf+'01') between cast(DATEADD(MM,-23,@periodo_maximo) as date) and cast(DATEADD(MM,0,@periodo_maximo) as date)
and iden = @p_Cuit
and not exists (select * from cendeu c2
where c2.iden = cendeu.iden
and cast(c2.codigo_ent as int) = cast(cendeu.codigo_ent as int)
and c2.fec_inf = cendeu.fec_inf
and c2.vigente = 1
and c2.id_fuente = 2
and c2.id_cendeu != cendeu.id_cendeu)
and not exists (select * from cendeu b2
where b2.iden = cendeu.iden
and cast(b2.codigo_ent as int) = cast(cendeu.codigo_ent as int)
and b2.fec_inf = cendeu.fec_inf
and b2.vigente = cendeu.vigente
and (isnull(b2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
and b2.id_cendeu < cendeu.id_cendeu)
and not (
(cast(fec_inf+'01' as date) < @p_5anios)
OR
(cast(fec_inf+'01' as date) < @p_2anios
and
not exists (select * from cendeu z2 where z2.iden = cendeu.iden and cast(z2.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z2.created) > @p_3meses)))
OR
(cast(fec_inf+'01' as date) < @p_2anios
and
exists (select * from cendeu_cancela d2
where d2.iden = cendeu.iden
and cast(d2.codigo_ent as int) = cast(cendeu.codigo_ent as int)
and cast(fec_inf+'01' as date) between convert(date,d2.fec_inf_desde) and convert(date,d2.fec_inf_hasta)
and d2.vigente = '1'))
)
group by prestamos,garotor,garprefa,garprefb,singarpref,cgarprefa,cgarprefb,sincgarpre,fec_inf
union all
select convert(int,sum(cgarprefa+garprefa)) con_garantias_a,convert(int,sum(cgarprefb+garprefb)) as con_garantias_b,
convert(int,sum(sincgarpre+SINgarpref)) sin_garantias,(convert(int,sum(prestamos+particip+garotor+o_cptos))) total,convert(date,fec_inf+'01') as periodo from cendeu_temp
where convert(date,fec_inf+'01') between cast(DATEADD(MM,-23,@periodo_maximo) as date) and cast(DATEADD(MM,0,@periodo_maximo) as date)
and iden = @p_Cuit
and not exists (select * from cendeu_temp c2
where c2.iden = cendeu_temp.iden
and cast(c2.codigo_ent as int) = cast(cendeu_temp.codigo_ent as int)
and c2.fec_inf = cendeu_temp.fec_inf
and c2.vigente = 1
and c2.id_fuente = 2
and c2.id_cendeu != cendeu_temp.id_cendeu)
and not exists (select * from cendeu b2
where b2.iden = cendeu_temp.iden
and cast(b2.codigo_ent as int) = cast(cendeu_temp.codigo_ent as int)
and b2.fec_inf = cendeu_temp.fec_inf
and b2.vigente = cendeu_temp.vigente
and (isnull(b2.id_fuente,1)) = (isnull(cendeu_temp.id_fuente,1))
and b2.id_cendeu < cendeu_temp.id_cendeu)
and not (
(cast(fec_inf+'01' as date) < @p_5anios)
OR
(cast(fec_inf+'01' as date) < @p_2anios
and
not exists (select * from cendeu z2 where z2.iden = cendeu_temp.iden and cast(z2.codigo_ent as int) = cast(cendeu_temp.codigo_ent as int) and (convert(date,z2.created) > @p_3meses)))
OR
(cast(fec_inf+'01' as date) < @p_2anios
and
exists (select * from cendeu_cancela d2
where d2.iden = cendeu_temp.iden
and cast(d2.codigo_ent as int) = cast(cendeu_temp.codigo_ent as int)
and cast(fec_inf+'01' as date) between convert(date,d2.fec_inf_desde) and convert(date,d2.fec_inf_hasta)
and d2.vigente = '1'))
)
group by prestamos,garotor,garprefa,garprefb,singarpref,cgarprefa,cgarprefb,sincgarpre,fec_inf
order by periodo asc
end
END
GO
GO
CREATE OR ALTER PROCEDURE dbo.obtener_garantias_bancos_mas_singarantias
@p_Cuit varchar(20)
AS
BEGIN
SET NOCOUNT ON;
declare @p_5anios date = getdate() - (5*365)
declare @p_2anios date = getdate() - (2*365)
declare @p_3meses date = getdate() - (90)
declare @periodo_maximo date,@periodo_maximo_cuit date,@fecha_formateada varchar(10),@periodo_comparar varchar(6);
select @periodo_maximo = (select max(fecha_informacion) from fuentes_procesadas where visible = '1' and fuente ='cendeu')
select @periodo_maximo_cuit = convert(date,isnull(max(cendeu.fec_inf),'200112')+'01') from cendeu where iden = @p_cuit AND vigente = 1
if (@periodo_maximo_cuit > @periodo_maximo)
begin
set @periodo_maximo = @periodo_maximo_cuit
end
if @periodo_maximo is not null
begin
select convert(int,sum(cgarprefa+garprefa)) con_garantias_a,convert(int,sum(cgarprefb+garprefb)) as con_garantias_b,
convert(int,sum(sincgarpre+SINgarpref)) sin_garantias,(convert(int,sum(prestamos+particip+garotor+o_cptos))) total,convert(date,fec_inf+'01') as periodo from cendeu
where convert(date,fec_inf+'01') between cast(DATEADD(MM,-23,@periodo_maximo) as date) and cast(DATEADD(MM,0,@periodo_maximo) as date)
and iden = @p_Cuit
and not exists (select * from cendeu c2
where c2.iden = cendeu.iden
and cast(c2.codigo_ent as int) = cast(cendeu.codigo_ent as int)
and c2.fec_inf = cendeu.fec_inf
and c2.vigente = 1
and c2.id_fuente = 2
and c2.id_cendeu != cendeu.id_cendeu)
and not exists (select * from cendeu b2
where b2.iden = cendeu.iden
and cast(b2.codigo_ent as int) = cast(cendeu.codigo_ent as int)
and b2.fec_inf = cendeu.fec_inf
and b2.vigente = cendeu.vigente
and (isnull(b2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
and b2.id_cendeu < cendeu.id_cendeu)
and not (
(cast(fec_inf+'01' as date) < @p_5anios)
OR
(cast(fec_inf+'01' as date) < @p_2anios
and
not exists (select * from cendeu z2 where z2.iden = cendeu.iden and cast(z2.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z2.created) > @p_3meses)))
OR
(cast(fec_inf+'01' as date) < @p_2anios
and
exists (select * from cendeu_cancela d2
where d2.iden = cendeu.iden
and cast(d2.codigo_ent as int) = cast(cendeu.codigo_ent as int)
and cast(fec_inf+'01' as date) between convert(date,d2.fec_inf_desde) and convert(date,d2.fec_inf_hasta)
and d2.vigente = '1'))
)
group by prestamos,garotor,garprefa,garprefb,singarpref,cgarprefa,cgarprefb,sincgarpre,fec_inf
union all
select convert(int,sum(cgarprefa+garprefa)) con_garantias_a,convert(int,sum(cgarprefb+garprefb)) as con_garantias_b,
convert(int,sum(sincgarpre+SINgarpref)) sin_garantias,(convert(int,sum(prestamos+particip+garotor+o_cptos))) total,convert(date,fec_inf+'01') as periodo from cendeu_temp
where convert(date,fec_inf+'01') between cast(DATEADD(MM,-23,@periodo_maximo) as date) and cast(DATEADD(MM,0,@periodo_maximo) as date)
and iden = @p_Cuit
and not exists (select * from cendeu_temp c2
where c2.iden = cendeu_temp.iden
and cast(c2.codigo_ent as int) = cast(cendeu_temp.codigo_ent as int)
and c2.fec_inf = cendeu_temp.fec_inf
and c2.vigente = 1
and c2.id_fuente = 2
and c2.id_cendeu != cendeu_temp.id_cendeu)
and not exists (select * from cendeu b2
where b2.iden = cendeu_temp.iden
and cast(b2.codigo_ent as int) = cast(cendeu_temp.codigo_ent as int)
and b2.fec_inf = cendeu_temp.fec_inf
and b2.vigente = cendeu_temp.vigente
and (isnull(b2.id_fuente,1)) = (isnull(cendeu_temp.id_fuente,1))
and b2.id_cendeu < cendeu_temp.id_cendeu)
and not (
(cast(fec_inf+'01' as date) < @p_5anios)
OR
(cast(fec_inf+'01' as date) < @p_2anios
and
not exists (select * from cendeu z2 where z2.iden = cendeu_temp.iden and cast(z2.codigo_ent as int) = cast(cendeu_temp.codigo_ent as int) and (convert(date,z2.created) > @p_3meses)))
OR
(cast(fec_inf+'01' as date) < @p_2anios
and
exists (select * from cendeu_cancela d2
where d2.iden = cendeu_temp.iden
and cast(d2.codigo_ent as int) = cast(cendeu_temp.codigo_ent as int)
and cast(fec_inf+'01' as date) between convert(date,d2.fec_inf_desde) and convert(date,d2.fec_inf_hasta)
and d2.vigente = '1'))
)
group by prestamos,garotor,garprefa,garprefb,singarpref,cgarprefa,cgarprefb,sincgarpre,fec_inf
order by periodo asc
end
END
GO
Used By
No items found