I've been working in a time attendance application and need some enlightment.
This is my current SQL query:
SELECT M.IdMarcacao, M.IdFuncionario, M.Data, M.Hora, Extra,
(M.Hora-(convert(varchar(11),dateadd(ms,cast(Extra*3600000 as bigint),'12/30/1899'),108))) as teste
FROM TimeReport.dbo.Marcacoes M
INNER JOIN TimeReport.dbo.Resultados R ON M.IdFuncionario = R.IdFuncionario
AND M.Data = R.Data
WHERE (R.Extra <> 0 AND M.[Tipo Marcacao] = 'SAI')
AND M.Hora=(SELECT max(hora)
FROM timereport.dbo.marcacoes
WHERE data = M.Data)
This returns the lines where:
1 - The person has made overtime hours.
2 - The last logout time
3 - The total of overtime hours.
4 - The difference between logout time and overtime = schedule time
5 - Make sure it's a logout time (Tipo = SAI)
Without entering into much detail about the application itself, what I really need is to turn this into an UPDATE
statement.
I used to do this:
UPDATE [TimeReport].[dbo].[Marcacoes]
SET [Hora] = [Hora] - convert(datetime,'01:00:00',108)
WHERE [Hora] > '1899-12-30 19:00:00.000'
For every single hour until 0pm :( It's not a good solution I know.
This update, will change the original logout time to minus 1 hour if the worker left at 19h, when the company schedule is 18h.
What I am trying to do, is simplify and automate the process.
Which brings my previous question... How can I do a update based on that select statement?
Or... in other words, something like:
UPDATE TimeReport.dbo.Marcacoes
SET Hora = (The value from the statement above, field "teste")
WHERE IdMarcacao = IdMarcacao(from the statement above)
Note: This "IdMarcacao" is a unique identifier for the row.
Thank you!