dbo.sp_balances_2007_grupos_empleados_ultimo

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

CREATE OR ALTER PROCEDURE dbo.sp_balances_2007_grupos_empleados_ultimo 
AS
BEGIN
    SET NOCOUNT ON;
 declare @id int, 
         @cuit varchar(20),
   @fecha_balance date,
   @desde char(4), 
   @hasta char(4),
   @anio int,
   @tabla1 varchar(100), 
   @tabla2 varchar(100), 
   @periodo1 int, 
   @periodo2 int,
   @cant_empleados int, 
   @sueldos decimal(20,2)

 declare @ultimo_mes_empleados date,
   @mes_empleados date,
   @periodo_empleados_desde  int,
   @periodo_empleados_hasta int,
   @cantidad int

 set @ultimo_mes_empleados = (select max(fuentes_procesadas.fecha_informacion) from fuentes_procesadas where fuente = 'empleados')
 set @mes_empleados = DATEADD(M,-2,@ultimo_mes_empleados)
 set @periodo_empleados_desde = convert(int,substring(convert(char,@mes_empleados),3,2)+substring(convert(char,@mes_empleados),6,2))
 set @periodo_empleados_hasta = convert(int,substring(convert(char,@ultimo_mes_empleados),3,2)+substring(convert(char,@ultimo_mes_empleados),6,2))

 DECLARE C_balances CURSOR FOR
  select id, cuit, fecha_balance
    from balances_por_grupo 
   --where cuit = '30709723274'
   order by id

 OPEN C_balances
 FETCH NEXT FROM C_balances INTO @id, @cuit, @fecha_balance
 WHILE @@fetch_status = 0
 BEGIN
     -- Busco de los ultimos 3 meses, que mes trajo mas empleados 
  set @periodo1 = null
     
  select top 1 @periodo1 = periodo, @cantidad = count(distinct cuil)
   from empleados 
   where empleados.cuit = @cuit
   and periodo between @periodo_empleados_desde and @periodo_empleados_hasta
   and CONVERT(VARCHAR,periodo) not like '%12' 
   and CONVERT(VARCHAR,periodo) not like '%06'
   group by periodo
   order by 2 desc, 1 desc

  if (@periodo1 is not null) 
  begin
   
   set @cant_empleados = (select count(distinct cuil) from empleados where cuit = @cuit and periodo = @periodo1)
   set @sueldos        = (select sum(remimp) from empleados  a
         where a.cuit = @cuit 
         and a.periodo = @periodo1
         and not exists (select * from empleados b
              where b.cuit = a.cuit 
              and b.cuil = a.cuil
              and b.periodo = a.periodo 
              and b.id_empleado > a.id_empleado)) 
    /*
       select @cant_empleados = count(cuil),
                     @sueldos = sum(remimp) 
     from empleados a 
        where a.cuit = @cuit 
      and a.periodo = @periodo1
          and id_empleado  in (select id_empleado 
                                 from empleados b
        where b.cuit = a.cuit 
      and b.cuil = a.cuil
      and b.periodo = a.periodo
      and b.created = (select max(created) 
                        from empleados c
                       where b.cuit = c.cuit 
                         and b.cuil = c.cuil
                         and b.periodo = c.periodo)
       )
     */
       
   update balances_por_grupo
      set cant_empleados_ultimo = @cant_empleados, sueldos_ultimo = @sueldos, empleados_periodo_ultimo = @periodo1 where id = @id 
  end 

  FETCH NEXT FROM C_balances INTO @id, @cuit, @fecha_balance
 END  
 CLOSE C_Balances
 DEALLOCATE C_Balances

END 

GO

Used By

No items found