Skip to main content
Fim da Recompensa com 50 pontos de reputação concedidos por Otácio Barbosa
removeu 2 caracteres do conteúdo
Fonte Link
Sorack
  • 26,5mil
  • 6
  • 43
  • 96
if object_id('dbo.batidas', 'TF') is null
begin
  exec('create function dbo.batidas() returns @retorno table(T int) as begin return end');
end;
go

createalter function dbo.batidas(@data_inicio date,
                            @data_final date)
returns @retorno table(chapa      varchar(100),
                       nome       varchar(100),
                       filial     varchar(100),
                       secao      varchar(100),
                       situacao   varchar(100),
                       [data 1]   date,
                       [data 2]   date,
                       [data 3]   date,
                       ocorrencia varchar(100))
as
begin
  declare @batidas table(filial    varchar(100),
                         coligada  varchar(100),
                         chapa     varchar(100),
                         secao     varchar(100),
                         situacao  varchar(100),
                         nome      varchar(100),
                         data      datetime,
                         dia       varchar(100),
                         batida    int);

  insert into @batidas(filial,
                       coligada,
                       chapa,
                       secao,
                       situacao,
                       nome,
                       data,
                       dia,
                       batida)
  select f.filial,
         f.coligada,
         f.chapa,
         f.secao,
         f.situacao,
         f.nome,
         v.data,
         datename(dw,v.data),
         max(v.sequencialbatida)
    from arelbatidatransitoview as v
    left join vwfunc  as f on v.chapa = f.chapa
   where datepart(dw,v.data) = 1  -- domingo
     and batida is not null
     and  v.data between @data_inicio and @data_final
     and v.codcoligada = 1
   group by v.chapa,
            v.data,
            f.filial,
            f.coligada,
            f.chapa,
            f.secao,
            f.funcao,
            f.nome,
            f.situacao;

   insert into @retorno (chapa,
                         nome,
                         filial,
                         secao,
                         situacao,
                         [data 1],
                         [data 2],
                         [data 3],
                         ocorrencia)
  select t1.chapa,
         t1.nome,
         t1.filial,
         t1.secao,
         t1.situacao,
         cast(t1.data as date),
         cast(t2.data as date),
         cast(t3.data as date),
         '04 - TESTE DOMINGO'
  from @batidas as t1
 inner join @batidas as t2 on t2.chapa = t1.chapa
 inner join @batidas as t3 on t3.chapa = t1.chapa
 where t2.data = dateadd(day, + 7, t1.data)
   and t3.data = dateadd(day, + 14, t1.data);

  return;
end;
go
if object_id('dbo.batidas', 'TF') is null
begin
  exec('create function dbo.batidas() returns @retorno table(T int) as begin return end');
end;
go

create function dbo.batidas(@data_inicio date,
                            @data_final date)
returns @retorno table(chapa      varchar(100),
                       nome       varchar(100),
                       filial     varchar(100),
                       secao      varchar(100),
                       situacao   varchar(100),
                       [data 1]   date,
                       [data 2]   date,
                       [data 3]   date,
                       ocorrencia varchar(100))
as
begin
  declare @batidas table(filial    varchar(100),
                         coligada  varchar(100),
                         chapa     varchar(100),
                         secao     varchar(100),
                         situacao  varchar(100),
                         nome      varchar(100),
                         data      datetime,
                         dia       varchar(100),
                         batida    int);

  insert into @batidas(filial,
                       coligada,
                       chapa,
                       secao,
                       situacao,
                       nome,
                       data,
                       dia,
                       batida)
  select f.filial,
         f.coligada,
         f.chapa,
         f.secao,
         f.situacao,
         f.nome,
         v.data,
         datename(dw,v.data),
         max(v.sequencialbatida)
    from arelbatidatransitoview as v
    left join vwfunc  as f on v.chapa = f.chapa
   where datepart(dw,v.data) = 1  -- domingo
     and batida is not null
     and  v.data between @data_inicio and @data_final
     and v.codcoligada = 1
   group by v.chapa,
            v.data,
            f.filial,
            f.coligada,
            f.chapa,
            f.secao,
            f.funcao,
            f.nome,
            f.situacao;

   insert into @retorno (chapa,
                         nome,
                         filial,
                         secao,
                         situacao,
                         [data 1],
                         [data 2],
                         [data 3],
                         ocorrencia)
  select t1.chapa,
         t1.nome,
         t1.filial,
         t1.secao,
         t1.situacao,
         cast(t1.data as date),
         cast(t2.data as date),
         cast(t3.data as date),
         '04 - TESTE DOMINGO'
  from @batidas as t1
 inner join @batidas as t2 on t2.chapa = t1.chapa
 inner join @batidas as t3 on t3.chapa = t1.chapa
 where t2.data = dateadd(day, + 7, t1.data)
   and t3.data = dateadd(day, + 14, t1.data);

  return;
end;
go
if object_id('dbo.batidas', 'TF') is null
begin
  exec('create function dbo.batidas() returns @retorno table(T int) as begin return end');
end;
go

alter function dbo.batidas(@data_inicio date,
                           @data_final date)
returns @retorno table(chapa      varchar(100),
                       nome       varchar(100),
                       filial     varchar(100),
                       secao      varchar(100),
                       situacao   varchar(100),
                       [data 1]   date,
                       [data 2]   date,
                       [data 3]   date,
                       ocorrencia varchar(100))
as
begin
  declare @batidas table(filial    varchar(100),
                         coligada  varchar(100),
                         chapa     varchar(100),
                         secao     varchar(100),
                         situacao  varchar(100),
                         nome      varchar(100),
                         data      datetime,
                         dia       varchar(100),
                         batida    int);

  insert into @batidas(filial,
                       coligada,
                       chapa,
                       secao,
                       situacao,
                       nome,
                       data,
                       dia,
                       batida)
  select f.filial,
         f.coligada,
         f.chapa,
         f.secao,
         f.situacao,
         f.nome,
         v.data,
         datename(dw,v.data),
         max(v.sequencialbatida)
    from arelbatidatransitoview as v
    left join vwfunc  as f on v.chapa = f.chapa
   where datepart(dw,v.data) = 1  -- domingo
     and batida is not null
     and  v.data between @data_inicio and @data_final
     and v.codcoligada = 1
   group by v.chapa,
            v.data,
            f.filial,
            f.coligada,
            f.chapa,
            f.secao,
            f.funcao,
            f.nome,
            f.situacao;

   insert into @retorno (chapa,
                         nome,
                         filial,
                         secao,
                         situacao,
                         [data 1],
                         [data 2],
                         [data 3],
                         ocorrencia)
  select t1.chapa,
         t1.nome,
         t1.filial,
         t1.secao,
         t1.situacao,
         cast(t1.data as date),
         cast(t2.data as date),
         cast(t3.data as date),
         '04 - TESTE DOMINGO'
  from @batidas as t1
 inner join @batidas as t2 on t2.chapa = t1.chapa
 inner join @batidas as t3 on t3.chapa = t1.chapa
 where t2.data = dateadd(day, + 7, t1.data)
   and t3.data = dateadd(day, + 14, t1.data);

  return;
end;
go
Alterado cabeçalho da função
Fonte Link
Sorack
  • 26,5mil
  • 6
  • 43
  • 96
if object_id('dbo.batidas', 'TF') is not null
begin
  dropexec('create function dbo.batidas;batidas() returns @retorno table(T int) as begin return end');
end;
go

create function dbo.batidas(@data_inicio date,
                            @data_final date)
returns @retorno table(chapa      varchar(100),
                       nome       varchar(100),
                       filial     varchar(100),
                       secao      varchar(100),
                       situacao   varchar(100),
                       [data 1]   date,
                       [data 2]   date,
                       [data 3]   date,
                       ocorrencia varchar(100))
as
begin
  declare @batidas table(filial    varchar(100),
                         coligada  varchar(100),
                         chapa     varchar(100),
                         secao     varchar(100),
                         situacao  varchar(100),
                         nome      varchar(100),
                         data      datetime,
                         dia       varchar(100),
                         batida    int);

  insert into @batidas(filial,
                       coligada,
                       chapa,
                       secao,
                       situacao,
                       nome,
                       data,
                       dia,
                       batida)
  select f.filial,
         f.coligada,
         f.chapa,
         f.secao,
         f.situacao,
         f.nome,
         v.data,
         datename(dw,v.data),
         max(v.sequencialbatida)
    from arelbatidatransitoview as v
    left join vwfunc  as f on v.chapa = f.chapa
   where datepart(dw,v.data) = 1  -- domingo
     and batida is not null
     and  v.data between @data_inicio and @data_final
     and v.codcoligada = 1
   group by v.chapa,
            v.data,
            f.filial,
            f.coligada,
            f.chapa,
            f.secao,
            f.funcao,
            f.nome,
            f.situacao;

   insert into @retorno (chapa,
                         nome,
                         filial,
                         secao,
                         situacao,
                         [data 1],
                         [data 2],
                         [data 3],
                         ocorrencia)
  select t1.chapa,
         t1.nome,
         t1.filial,
         t1.secao,
         t1.situacao,
         cast(t1.data as date),
         cast(t2.data as date),
         cast(t3.data as date),
         '04 - TESTE DOMINGO'
  from @batidas as t1
 inner join @batidas as t2 on t2.chapa = t1.chapa
 inner join @batidas as t3 on t3.chapa = t1.chapa
 where t2.data = dateadd(day, + 7, t1.data)
   and t3.data = dateadd(day, + 14, t1.data);

  return;
end;
go
if object_id('dbo.batidas', 'TF') is not null
begin
  drop function dbo.batidas;
end;
go

create function dbo.batidas(@data_inicio date,
                            @data_final date)
returns @retorno table(chapa      varchar(100),
                       nome       varchar(100),
                       filial     varchar(100),
                       secao      varchar(100),
                       situacao   varchar(100),
                       [data 1]   date,
                       [data 2]   date,
                       [data 3]   date,
                       ocorrencia varchar(100))
as
begin
  declare @batidas table(filial    varchar(100),
                         coligada  varchar(100),
                         chapa     varchar(100),
                         secao     varchar(100),
                         situacao  varchar(100),
                         nome      varchar(100),
                         data      datetime,
                         dia       varchar(100),
                         batida    int);

  insert into @batidas(filial,
                       coligada,
                       chapa,
                       secao,
                       situacao,
                       nome,
                       data,
                       dia,
                       batida)
  select f.filial,
         f.coligada,
         f.chapa,
         f.secao,
         f.situacao,
         f.nome,
         v.data,
         datename(dw,v.data),
         max(v.sequencialbatida)
    from arelbatidatransitoview as v
    left join vwfunc  as f on v.chapa = f.chapa
   where datepart(dw,v.data) = 1  -- domingo
     and batida is not null
     and  v.data between @data_inicio and @data_final
     and v.codcoligada = 1
   group by v.chapa,
            v.data,
            f.filial,
            f.coligada,
            f.chapa,
            f.secao,
            f.funcao,
            f.nome,
            f.situacao;

   insert into @retorno (chapa,
                         nome,
                         filial,
                         secao,
                         situacao,
                         [data 1],
                         [data 2],
                         [data 3],
                         ocorrencia)
  select t1.chapa,
         t1.nome,
         t1.filial,
         t1.secao,
         t1.situacao,
         cast(t1.data as date),
         cast(t2.data as date),
         cast(t3.data as date),
         '04 - TESTE DOMINGO'
  from @batidas as t1
 inner join @batidas as t2 on t2.chapa = t1.chapa
 inner join @batidas as t3 on t3.chapa = t1.chapa
 where t2.data = dateadd(day, + 7, t1.data)
   and t3.data = dateadd(day, + 14, t1.data);

  return;
end;
go
if object_id('dbo.batidas', 'TF') is null
begin
  exec('create function dbo.batidas() returns @retorno table(T int) as begin return end');
end;
go

create function dbo.batidas(@data_inicio date,
                            @data_final date)
returns @retorno table(chapa      varchar(100),
                       nome       varchar(100),
                       filial     varchar(100),
                       secao      varchar(100),
                       situacao   varchar(100),
                       [data 1]   date,
                       [data 2]   date,
                       [data 3]   date,
                       ocorrencia varchar(100))
as
begin
  declare @batidas table(filial    varchar(100),
                         coligada  varchar(100),
                         chapa     varchar(100),
                         secao     varchar(100),
                         situacao  varchar(100),
                         nome      varchar(100),
                         data      datetime,
                         dia       varchar(100),
                         batida    int);

  insert into @batidas(filial,
                       coligada,
                       chapa,
                       secao,
                       situacao,
                       nome,
                       data,
                       dia,
                       batida)
  select f.filial,
         f.coligada,
         f.chapa,
         f.secao,
         f.situacao,
         f.nome,
         v.data,
         datename(dw,v.data),
         max(v.sequencialbatida)
    from arelbatidatransitoview as v
    left join vwfunc  as f on v.chapa = f.chapa
   where datepart(dw,v.data) = 1  -- domingo
     and batida is not null
     and  v.data between @data_inicio and @data_final
     and v.codcoligada = 1
   group by v.chapa,
            v.data,
            f.filial,
            f.coligada,
            f.chapa,
            f.secao,
            f.funcao,
            f.nome,
            f.situacao;

   insert into @retorno (chapa,
                         nome,
                         filial,
                         secao,
                         situacao,
                         [data 1],
                         [data 2],
                         [data 3],
                         ocorrencia)
  select t1.chapa,
         t1.nome,
         t1.filial,
         t1.secao,
         t1.situacao,
         cast(t1.data as date),
         cast(t2.data as date),
         cast(t3.data as date),
         '04 - TESTE DOMINGO'
  from @batidas as t1
 inner join @batidas as t2 on t2.chapa = t1.chapa
 inner join @batidas as t3 on t3.chapa = t1.chapa
 where t2.data = dateadd(day, + 7, t1.data)
   and t3.data = dateadd(day, + 14, t1.data);

  return;
end;
go
Fonte Link
Sorack
  • 26,5mil
  • 6
  • 43
  • 96

Execute a seguinte função no seu banco de dados:

if object_id('dbo.batidas', 'TF') is not null
begin
  drop function dbo.batidas;
end;
go

create function dbo.batidas(@data_inicio date,
                            @data_final date)
returns @retorno table(chapa      varchar(100),
                       nome       varchar(100),
                       filial     varchar(100),
                       secao      varchar(100),
                       situacao   varchar(100),
                       [data 1]   date,
                       [data 2]   date,
                       [data 3]   date,
                       ocorrencia varchar(100))
as
begin
  declare @batidas table(filial    varchar(100),
                         coligada  varchar(100),
                         chapa     varchar(100),
                         secao     varchar(100),
                         situacao  varchar(100),
                         nome      varchar(100),
                         data      datetime,
                         dia       varchar(100),
                         batida    int);

  insert into @batidas(filial,
                       coligada,
                       chapa,
                       secao,
                       situacao,
                       nome,
                       data,
                       dia,
                       batida)
  select f.filial,
         f.coligada,
         f.chapa,
         f.secao,
         f.situacao,
         f.nome,
         v.data,
         datename(dw,v.data),
         max(v.sequencialbatida)
    from arelbatidatransitoview as v
    left join vwfunc  as f on v.chapa = f.chapa
   where datepart(dw,v.data) = 1  -- domingo
     and batida is not null
     and  v.data between @data_inicio and @data_final
     and v.codcoligada = 1
   group by v.chapa,
            v.data,
            f.filial,
            f.coligada,
            f.chapa,
            f.secao,
            f.funcao,
            f.nome,
            f.situacao;

   insert into @retorno (chapa,
                         nome,
                         filial,
                         secao,
                         situacao,
                         [data 1],
                         [data 2],
                         [data 3],
                         ocorrencia)
  select t1.chapa,
         t1.nome,
         t1.filial,
         t1.secao,
         t1.situacao,
         cast(t1.data as date),
         cast(t2.data as date),
         cast(t3.data as date),
         '04 - TESTE DOMINGO'
  from @batidas as t1
 inner join @batidas as t2 on t2.chapa = t1.chapa
 inner join @batidas as t3 on t3.chapa = t1.chapa
 where t2.data = dateadd(day, + 7, t1.data)
   and t3.data = dateadd(day, + 14, t1.data);

  return;
end;
go

Esta função é baseada no seu select anterior. Note que não é necessário criar tabelas temporárias quando podemos apenas utilizar variáveis do tipo tabela. O objetivo da função é justamente o que você queria fazer na query: buscar e organizar dados de forma sistemática. Após executá-la no banco, substitua a select abaixo na query:

select bat.CHAPA,
       bat.NOME,
       bat.FILIAL,
       bat.SECAO,
       bat.SITUACAO,
       bat.[DATA 1],
       bat.[DATA 2],
       bat.[DATA 3],
       bat.OCORRENCIA
  from dbo.batidas('$v_datainicio', '$v_datafinal') bat