Try declare a variable for the purpose:
DECLARE @Result Decimal(18, 0)
SET @Result = ROUND(SUM(DATEDIFF(ss,'2014-05-15 00:23:04.580','2014-05-15 00:25:04.580') / 60.0 / 60.0), 2)
update CheckInCheckOut
set CountHours= @Result
where UserId_Fk=3
Question
On run, gives error
An aggregate may not appear in the set list of an UPDATE statement.
Code:
update CheckInCheckOut
set CountHours = (select ROUND(SUM(DATEDIFF(ss,CheckIn,CheckOut) / 60.0 / 60.0), 2))
where UserId_Fk = 3
This query works when I write it as:
update CheckInCheckOut
set CountHours = ROUND(SUM(DATEDIFF(ss, '2014-05-15 00:23:04.580', '2014-05-15 00:25:04.580') / 60.0 / 60.0), 2)
CheckIn datetime,
CheckOut datetime
Example would be appreciated.
La solution
Try declare a variable for the purpose:
DECLARE @Result Decimal(18, 0)
SET @Result = ROUND(SUM(DATEDIFF(ss,'2014-05-15 00:23:04.580','2014-05-15 00:25:04.580') / 60.0 / 60.0), 2)
update CheckInCheckOut
set CountHours= @Result
where UserId_Fk=3