I want to compare the ALIAS hours with the ALIAS PlanWorkHours and assign the result to a new variable, however this is not possible since these ALIAS are temporary or do not represent as such a column of the table, there is some way to implement this solution.
if horasMes>PlanWorkHours
HorasExtra=PlanWorkHours-horasMes
select DISTINCT c.CodeCompany AS CompanyCode,
CONVERT(char(6), GETDATE(), 112) AS Period,
a.idClave as EmployeID,
c.nombre + ' ' + c.apellidos as EmployeName,
d.idDepto as Depto,
t.HorasAlMes as PlanWorkHours,
SUM(DATEDIFF(HOUR, fechaEntrada, fechaSalida) %24) AS horasMes,
abs(t.HorasAlMes-SUM(DATEDIFF(HOUR, fechaEntrada, fechaSalida) %24)) as Resta
from tblasistencia a JOIN tblpersonal c ON a.idClave=c.idClave
JOIN tblDepto d ON c.fkDepto=d.idDepto
JOIN tblTurno t ON c.fkTurno=t.idTurno
where
fechaEntrada BETWEEN CONVERT(DATETIME, '2017-11-01 00:00:00',121) and
CONVERT(DATETIME,'2017-11-30 23:59:59',121)and c.fkStatus !=1
group by
c.CodeCompany,
a.idClave,
d.idDepto,
t.HorasAlMes,
c.nombre + ' ' + c.apellidos