Total SUM time in SQL Server

I have a problem with the SUM statement in SQL Server. When I write this query:

 SELECT StudentID, StudentName, SUM(t2.time) as 'TotalTime' FROM WorkNote GROUP BY StudentID, StudentName 

I get this error:

The time operator of the time data is not valid for the sum operator.

Is there any other method for SUM or for calculating the total time from records?

In my WorkNote table, the WorkNote type time is Time(7) , and I want to SUM all (total) time for each student.

+6
source share
2 answers

if time is in hh/mm/ss then ::

 SELECT studentid,studentname, DATEADD(ms, SUM(DATEDIFF(ms, '00:00:00.000', mytime)), '00:00:00.000') as time FROM worknote 
+10
source

You can turn the time in seconds and sum it up.

 SELECT StudentID, StudentName, sum( DATEPART(SECOND, [time]) + 60 * DATEPART(MINUTE, [time]) + 3600 * DATEPART(HOUR, [time] ) ) as 'TotalTime' FROM WorkNote GROUP BY StudentID, StudentName 
+1
source

Source: https://habr.com/ru/post/956533/


All Articles