0
I have the following appointment:
with
Consulta as (
SELECT
V.CHAPA,
F.NOME,
V.DATA,
F.FILIAL,
DATENAME(dw,V.DATA) AS DIA,
MAX(V.SEQUENCIALBATIDA) AS BATIDA
FROM
V_BATIDASANTIGAS AS V
LEFT JOIN V_DADOSFUNC AS F ON V.CHAPA = F.CHAPA
WHERE
DATEPART(dw,V.DATA) = 1
AND BATIDA IS NOT NULL
AND V.CODCOLIGADA = 1
AND YEAR ( V.DATA ) = 2016
GROUP BY V.CHAPA,
F.NOME,
F.FILIAL,
V.DATA
),
ConsultaSeq as (
SELECT *, Seq= row_number() over (partition by CHAPA order by DATA)
from Consulta
)
SELECT
C1.CHAPA,
C1.NOME,
C1.FILIAL,
C1.DATA,
C2.DATA,
DateDiff(day, C1.DATA, C2.DATA)as DIAS,
'04 - MAIS DE 2 DOMINGOS SEM FOLGA ' AS DESCRICAO
from ConsultaSeq as C1
left join ConsultaSeq as C2 on C1.CHAPA = C2.CHAPA and C1.Seq = C2.Seq -1
where DateDiff(day, C1.DATA, C2.DATA) > 1
GROUP BY C1.CHAPA,
C1.NOME,
C1.FILIAL,
C1.DATA,
C2.DATA
He returns to me:
1234 JOAO DA SILVA 1 2016-01-10 00:00:00.000 2016-01-17 00:00:00.000 7 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-01-17 00:00:00.000 2016-01-31 00:00:00.000 14 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-01-31 00:00:00.000 2016-02-07 00:00:00.000 7 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-02-07 00:00:00.000 2016-02-21 00:00:00.000 14 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-02-21 00:00:00.000 2016-02-28 00:00:00.000 7 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-02-28 00:00:00.000 2016-03-13 00:00:00.000 14 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-03-13 00:00:00.000 2016-03-20 00:00:00.000 7 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-03-20 00:00:00.000 2016-05-08 00:00:00.000 49 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-05-08 00:00:00.000 2016-05-29 00:00:00.000 21 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-05-29 00:00:00.000 2016-06-05 00:00:00.000 7 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-06-05 00:00:00.000 2016-06-19 00:00:00.000 14 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-06-19 00:00:00.000 2016-06-26 00:00:00.000 7 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-06-26 00:00:00.000 2016-07-17 00:00:00.000 21 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-07-17 00:00:00.000 2016-07-24 00:00:00.000 7 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-07-24 00:00:00.000 2016-08-07 00:00:00.000 14 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-08-07 00:00:00.000 2016-08-14 00:00:00.000 7 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-08-14 00:00:00.000 2016-08-28 00:00:00.000 14 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-08-28 00:00:00.000 2016-09-04 00:00:00.000 7 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-09-04 00:00:00.000 2016-09-25 00:00:00.000 21 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-09-25 00:00:00.000 2016-10-02 00:00:00.000 7 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-10-02 00:00:00.000 2016-10-16 00:00:00.000 14 04 - MAIS DE 2 DOMINGOS SEM FOLGA
What I need:
Based on the month of the date only the results that have 7 3x consecutive are displayed.
Example:
1234 JOAO DA SILVA 1 2016-01-10 00:00:00.000 2016-01-17 00:00:00.000 7 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-01-17 00:00:00.000 2016-01-31 00:00:00.000 7 04 - MAIS DE 2 DOMINGOS SEM FOLGA
1234 JOAO DA SILVA 1 2016-01-31 00:00:00.000 2016-02-07 00:00:00.000 7 04 - MAIS DE 2 DOMINGOS SEM FOLGA
How could I parameterize this ?
NOTE: I can also read and save the new result in a table,I say because if you simpler meets me too.
Otácio, can there be more than one employee with the same PLAQUE value, if they are from different branches? The example does not match the request; you could check?
– José Diz
No, the plate is like the employee’s primary key.
– Chefe Druida
Next, which logic is following because returned empty, just so I understand the code and can validate
– Chefe Druida
Otácio, the logic is as follows: considering the objective "MORE THAN 2 SUNDAYS WITHOUT SLACK", for each line existing in #Tabbatidas (only Sundays), it is verified if there are beats in the next two Sundays. It is in the WHERE clause: +7 days and + 14 days
– José Diz
I got it, I’ll validate it and I’ll get back to you today,
– Chefe Druida