Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 sum of time values

Author  Topic 

anupalavila
Yak Posting Veteran

56 Posts

Posted - 2008-09-30 : 02:58:12
Hi

If in my table I have values like this

EmpId Duration
1 1900-01-01 07:49:03.000
2 1900-01-01 02:28:06.000
1 1900-01-01 03:09:01.000
1 1900-01-01 06:19:02.000

how can I calculate the total duration of EmpId 1 ie

07:49:03.000 + 03:09:01.000 + 06:19:02.000

please help me with the query

Thanks and Regards
Anu Palavila

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-30 : 03:42:12
[code]DECLARE @Sample TABLE
(
empID INT,
dur DATETIME
)

INSERT @Sample
SELECT 1, '1900-01-01 07:49:03.000' UNION ALL
SELECT 2, '1900-01-01 02:28:06.000' UNION ALL
SELECT 1, '1900-01-01 03:09:01.000' UNION ALL
SELECT 1, '1900-01-01 06:19:02.000'

SELECT empID,
DATEADD(MILLISECOND, SUM(DATEDIFF(MILLISECOND, 0, dur)), 0)
FROM @Sample
GROUP BY empID
ORDER BY empID

SELECT empID,
DATEADD(SECOND, SUM(DATEDIFF(SECOND, 0, dur)), 0)
FROM @Sample
GROUP BY empID
ORDER BY empID

SELECT empID,
SUM(DATEDIFF(SECOND, 0, dur)) AS Seconds
FROM @Sample
GROUP BY empID
ORDER BY empID

SELECT empID,
DATEADD(MINUTE, SUM(DATEDIFF(MINUTE, 0, dur)), 0)
FROM @Sample
GROUP BY empID
ORDER BY empID[/code]

E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-30 : 03:44:06
[code]select EmpId,
CAST(SUM(DATEDIFF(ss,0,Duration))/3600 AS varchar(2))+':'+CAST((SUM(DATEDIFF(ss,0,Duration))%3600)/60 AS varchar(2)) +':'+CAST((SUM(DATEDIFF(ss,0,Duration))%3600)%60 AS varchar(2))
FROm YourTable
GROUP BY EmpID[/code]
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-30 : 03:45:14
See http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=107275



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-09-30 : 09:47:47
This is a general solution to sum elapsed datetime values and return the sum as a datetime value that is an offset from 1900-01-01 00:00:00.000.
select
TotalElapsedTime =
dateadd(dd,sum(datediff(dd,0,a.ET)),
dateadd(hh,sum(datepart(hh,a.ET)),
dateadd(mi,sum(datepart(mi,a.ET)),
dateadd(ss,sum(datepart(ss,a.ET)),
dateadd(ms,sum(datepart(ms,a.ET)),0)))))
from
( -- Test Data
select ET = convert(datetime,'1900-01-01 07:49:03.000') union all
select ET = convert(datetime,'1900-01-01 02:28:06.000') union all
select ET = convert(datetime,'1900-01-01 03:09:01.003') union all
select ET = convert(datetime,'1900-01-01 06:19:02.040') union all
select ET = convert(datetime,'1900-01-07 06:19:02.000')
) a


Results:

TotalElapsedTime
------------------------------------------------------
1900-01-08 02:04:14.043

(1 row(s) affected)




CODO ERGO SUM
Go to Top of Page
   

- Advertisement -