dbo.obtener_garantias_bancos_new

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_new
@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 
 (select cast(DATEADD(MM,0,@periodo_maximo) as date)) as periodo,
 (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,0,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,0,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = 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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,0,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total


 union all
 select 
 (select cast(DATEADD(MM,-1,@periodo_maximo) as date)) as periodo,
 (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-1,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-1,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-1,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-1,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total


 union all
 select 
 (select cast(DATEADD(MM,-2,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-2,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-2,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-2,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-2,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 union all
 select 
 (select cast(DATEADD(MM,-3,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-3,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-3,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-3,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-3,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 union all
 select 
 (select cast(DATEADD(MM,-4,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-4,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-4,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-4,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-4,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 union all
 select 
 (select cast(DATEADD(MM,-5,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-5,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-5,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-5,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-5,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 union all
 select 
 (select cast(DATEADD(MM,-6,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-6,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-6,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-6,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-6,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 union all
 select 
 (select cast(DATEADD(MM,-7,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-7,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-7,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-7,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-7,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 union all
 select 
 (select cast(DATEADD(MM,-8,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-8,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-8,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-8,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-8,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 union all

 select 
 (select cast(DATEADD(MM,-9,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-9,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-9,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-9,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-9,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 
 union all
 select 
 (select cast(DATEADD(MM,-10,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-10,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-10,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-10,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-10,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 union all
 select 
 (select cast(DATEADD(MM,-11,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-11,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-11,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-11,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-11,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 union all
 select 
 (select cast(DATEADD(MM,-12,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-12,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-12,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-12,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-12,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 

 union all
 select 
 (select cast(DATEADD(MM,-13,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-13,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-13,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-13,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-13,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total

 union all
  select 
 (select cast(DATEADD(MM,-14,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-14,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-14,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-14,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-14,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total

 union all
 select 
 (select cast(DATEADD(MM,-15,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-15,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-15,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-15,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-15,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 union all
 select 
 (select cast(DATEADD(MM,-16,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-16,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-16,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-16,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-16,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 union all
 select 
 (select cast(DATEADD(MM,-17,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-17,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-17,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-17,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-17,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 union all
 select 
 (select cast(DATEADD(MM,-18,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-18,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-18,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-18,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-18,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 union all
 select 
 (select cast(DATEADD(MM,-19,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-19,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-19,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-19,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-19,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 union all
 select 
 (select cast(DATEADD(MM,-20,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-20,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-20,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-20,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-20,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 union all
 select 
 (select cast(DATEADD(MM,-21,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-21,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-21,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-21,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-21,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 union all
 select 
 (select cast(DATEADD(MM,-22,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-22,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-22,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-22,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-22,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 union all
 select 
 (select cast(DATEADD(MM,-23,@periodo_maximo) as date)) as periodo,
  (select (convert(int,sum(cgarprefa+garprefa))) from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-23,@periodo_maximo) as date) 
 and  iden = @p_Cuit
 and not exists (select * from cendeu c 
      where c.iden = cendeu.iden 
      and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c.fec_inf = cendeu.fec_inf 
      and c.vigente = 1 
      and c.id_fuente = 2 
      and c.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b 
      where b.iden = cendeu.iden
      and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b.fec_inf = cendeu.fec_inf
      and  b.vigente = cendeu.vigente
      and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b.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 z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d 
      where d.iden = cendeu.iden 
       and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
       and d.vigente = '1'))
    )
 group by fec_inf) con_garantias_a, 

 (select convert(int,sum(cgarprefb+garprefb)) as cgarprprefb from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-23,@periodo_maximo) as date) 
 and  iden = @p_Cuit and not exists (select * from cendeu c1 
      where c1.iden = cendeu.iden 
      and cast(c1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c1.fec_inf = cendeu.fec_inf 
      and c1.vigente = 1 
      and c1.id_fuente = 2 
      and c1.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b1
      where b1.iden = cendeu.iden
      and  cast(b1.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b1.fec_inf = cendeu.fec_inf
      and  b1.vigente = cendeu.vigente
      and (isnull(b1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b1.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 z1 where z1.iden = cendeu.iden and cast(z1.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z1.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d1 
      where d1.iden = cendeu.iden 
       and cast(d1.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d1.fec_inf_desde) and convert(date,d1.fec_inf_hasta)
       and d1.vigente = '1'))
    )
 group by fec_inf) con_garantias_b, 

 (select convert(int,sum(sincgarpre+singarpref)) as singarpref from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-23,@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 fec_inf) sin_garantias,

 (select (convert(int,sum(prestamos+particip+garotor+o_cptos))) as total from cendeu 
 where convert(date,fec_inf+'01') = cast(DATEADD(MM,-23,@periodo_maximo) as date) 
 and  iden = @p_Cuit 
  and not exists (select * from cendeu c3
      where c3.iden = cendeu.iden 
      and cast(c3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and c3.fec_inf = cendeu.fec_inf 
      and c3.vigente = 1 
      and c3.id_fuente = 2 
      and c3.id_cendeu != cendeu.id_cendeu)
 and not exists (select * from cendeu b3 
      where b3.iden = cendeu.iden
      and  cast(b3.codigo_ent as int) = cast(cendeu.codigo_ent as int)
      and  b3.fec_inf = cendeu.fec_inf
      and  b3.vigente = cendeu.vigente
      and (isnull(b3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
      and  b3.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 z3 where z3.iden = cendeu.iden and cast(z3.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z3.created) > @p_3meses)))
    OR 
    (cast(fec_inf+'01' as date) < @p_2anios 
    and 
    exists (select * from cendeu_cancela d3 
      where d3.iden = cendeu.iden 
       and cast(d3.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and cast(fec_inf+'01' as date) between convert(date,d3.fec_inf_desde) and convert(date,d3.fec_inf_hasta)
       and d3.vigente = '1'))
    )
 group by fec_inf) total
 
 order by periodo asc
 end
END

GO

Used By

No items found