dbo.obtener_situaciones_bancos

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
--exec [obtener_situaciones_bancos] '20133312065'
CREATE OR ALTER PROCEDURE dbo.obtener_situaciones_bancos
@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 @dtStart date, @dtEnd date, @dtEnd_cuit date,@periodo_maximo date,@fec_periodo_minimo varchar(11),@periodo_minimo varchar(7);
 
 select @dtEnd = (select max(fecha_informacion) from fuentes_procesadas where visible = '1' and fuente = 'cendeu')
 
 select @dtEnd_cuit = convert(date,isnull(max(cendeu.fec_inf),'200112')+'01') from cendeu where iden = @p_cuit and vigente = 1

 if (@dtEnd_cuit > @dtEnd) 
  begin 
     set @dtEnd = @dtEnd_cuit
  end 
 if @dtEnd is not null
 begin
  set @periodo_maximo = @dtEnd
-- mes 
 select 
 (select cast(DATEADD(MM,0,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,0,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,0,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,0,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,0,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,0,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,0,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6

 -- mes -1
 union all
 select 
 (select cast(DATEADD(MM,-1,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-1,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-1,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-1,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-1,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-1,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-1,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6
 union all

 -- mes -2
 
 select 
 (select cast(DATEADD(MM,-2,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-2,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-2,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-2,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-2,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-2,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-2,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6

 -- mes -3
  union all
 select 
 (select cast(DATEADD(MM,-3,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-3,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-3,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-3,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-3,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-3,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-3,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6

-- mes -4
 union all
 select 
 (select cast(DATEADD(MM,-4,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-4,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-4,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-4,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-4,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-4,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-4,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6

-- mes -5
 union all
 select 
 (select cast(DATEADD(MM,-5,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-5,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-5,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-5,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-5,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-5,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-5,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6
 
-- mes -6
 union all
 select 
 (select cast(DATEADD(MM,-6,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-6,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-6,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-6,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-6,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-6,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-6,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6

 
-- mes -7
 union all
 select 
 (select cast(DATEADD(MM,-7,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-7,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-7,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-7,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-7,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-7,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-7,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6

 
-- mes -8
 union all
 select 
 (select cast(DATEADD(MM,-8,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-8,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-8,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-8,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-8,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-8,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-8,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6

 
-- mes -9
 union all
 select 
 (select cast(DATEADD(MM,-9,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-9,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-9,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-9,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-9,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-9,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-9,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6

 
-- mes -10
 union all
 select 
 (select cast(DATEADD(MM,-10,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-10,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-10,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-10,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-10,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-10,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-10,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6

  
-- mes -11
 union all
 select 
 (select cast(DATEADD(MM,-11,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-11,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-11,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-11,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-11,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-11,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-11,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6


 
-- mes -12
 union all
 select 
 (select cast(DATEADD(MM,-12,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-12,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-12,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-12,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-12,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-12,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-12,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6



 
-- mes -13
 union all
 select 
 (select cast(DATEADD(MM,-13,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-13,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-13,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-13,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-13,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-13,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-13,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6



 
-- mes -14
 union all
 select 
 (select cast(DATEADD(MM,-14,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-14,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-14,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-14,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-14,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-14,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-14,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6



 
-- mes -15
 union all
 select 
 (select cast(DATEADD(MM,-15,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-15,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-15,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-15,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-15,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-15,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-15,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6



 
-- mes -16
 union all
 select 
 (select cast(DATEADD(MM,-16,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-16,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-16,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-16,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-16,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-16,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-16,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6



 
-- mes -17
 union all
 select 
 (select cast(DATEADD(MM,-17,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-17,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-17,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-17,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-17,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-17,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-17,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6



 
-- mes -18
 union all
 select 
 (select cast(DATEADD(MM,-18,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-18,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-18,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-18,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-18,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-18,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-18,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6


 
-- mes -19
 union all
 select 
 (select cast(DATEADD(MM,-19,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-19,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-19,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-19,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente 
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-19,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-19,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-19,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6

  
-- mes -20
 union all
 select 
 (select cast(DATEADD(MM,-20,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-20,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-20,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-20,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-20,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-20,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-20,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6

  
-- mes -21
 union all
 select 
 (select cast(DATEADD(MM,-21,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-21,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-21,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-21,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-21,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-21,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-21,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6

 
-- mes -22
 union all
 select 
 (select cast(DATEADD(MM,-22,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-22,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-22,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-22,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-22,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-22,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-22,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6


  
-- mes -23
 union all
 select 
 (select cast(DATEADD(MM,-23,@periodo_maximo) as date)) as periodo,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  from cendeu where convert(int,situacion) = 01 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-23,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
    and not exists (select * from cendeu c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
     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 c 
       where c.iden = cendeu.iden 
         and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
         and c.vigente = '1'))
       )
 group by fec_inf) sit1,
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 02 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-23,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu c2
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit2, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 03 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-23,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c3
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c3.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit3, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 04 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-23,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c4
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c4.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit4, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 05 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-23,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c5
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c5.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit5, 
 (select sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as situacion02 from cendeu where convert(int,situacion) = 06 and convert(date,fec_inf+'01') = cast(DATEADD(MM,-23,@periodo_maximo) as date) and  iden = @p_Cuit and vigente= 1   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 = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
      and not exists (select * from cendeu c6
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c6.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  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 (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    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 c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 group by fec_inf) sit6



 order by periodo asc
 end

END

GO

Used By

No items found