Saturday 12 May 2012

How to Show sum of Time from a Table for Total working Hours Calculation in Sql Server 2005

Create Given Below is Attendance Table :



Now Calculate Total Working Hours group by Personal_code. The OutPut Should be Like Given below :




For Getting this output use Given Below Query:


SELECT Personal_Code, convert(varchar(2),SUM(DATEDIFF(minute, '00:00:00', convert(datetime, totalTime, 8)))/60)+':'
+
case when len(convert(varchar(2),SUM(DATEDIFF(minute, '00:00:00', convert(datetime, totalTime, 8)))%60)) = 1
then
'0'+convert(varchar(2),SUM(DATEDIFF(minute, '00:00:00', convert(datetime, totalTime, 8)))%60)
else
convert(varchar(2),SUM(DATEDIFF(minute, '00:00:00', convert(datetime, totalTime, 8)))%60)
end
as totalTime
FROM Attandance
GROUP BY Personal_Code

No comments:

Post a Comment