Good evening, I need to make a query and I do not give with the one I need. I have a table with activities, which last, or not, more than one day, and in each activity 1 or up to 3 employees who perform the activity, it would be something like this
Actividad - fechaInicio - FechaFin - Empleado1 - Empleado2 - Empleado3
Now, I have to count how many days each activity lasts, I group them and calculate how many days each employee worked in the month, but only do it for an employee1, I do not add the employee2 nor the 3, I hit the query, for that is more graphic.
SELECT a.FechaInicio
,e.DiaAcuerdo
,SUM(datediff(day, a.FechaInicio, a.FechaFin)+1) as DiasTrabajados
,(e.DiaAcuerdo - sum (datediff (day, a.FechaInicio, a.FechaFin)+1)) as DiasRestantes
,(E.Nombre +' ' + E.Apellido) as Nombre
from Actividades as A
inner join Empleados E on
a.ID_Emp = E.ID_Emp
inner join Empleados E2 on
a.ID_Emp1 = E2.ID_Emp
inner join Empleados E3 on
a.ID_Emp1 = E3.ID_Emp
where DATEPART(month, A.FechaInicio) = DATEPART(month, getdate())
group by a.FechaInicio, a.FechaFin, a.ID_Emp,e.Nombre,e.Apellido,e.DiaAcuerdo
This query brings me
2018-06-03 20 18 2 Paula Alvarez
Could you give me a hand? Thank you!