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.
Author |
Topic |
anupalavila
Yak Posting Veteran
56 Posts |
Posted - 2008-09-30 : 02:58:12
|
HiIf 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.000how can I calculate the total duration of EmpId 1 ie07:49:03.000 + 03:09:01.000 + 06:19:02.000please help me with the queryThanks 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 @SampleSELECT 1, '1900-01-01 07:49:03.000' UNION ALLSELECT 2, '1900-01-01 02:28:06.000' UNION ALLSELECT 1, '1900-01-01 03:09:01.000' UNION ALLSELECT 1, '1900-01-01 06:19:02.000'SELECT empID, DATEADD(MILLISECOND, SUM(DATEDIFF(MILLISECOND, 0, dur)), 0)FROM @SampleGROUP BY empIDORDER BY empIDSELECT empID, DATEADD(SECOND, SUM(DATEDIFF(SECOND, 0, dur)), 0)FROM @SampleGROUP BY empIDORDER BY empIDSELECT empID, SUM(DATEDIFF(SECOND, 0, dur)) AS SecondsFROM @SampleGROUP BY empIDORDER BY empIDSELECT empID, DATEADD(MINUTE, SUM(DATEDIFF(MINUTE, 0, dur)), 0)FROM @SampleGROUP BY empIDORDER BY empID[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
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 YourTableGROUP BY EmpID[/code] |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
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') ) aResults:TotalElapsedTime ------------------------------------------------------ 1900-01-08 02:04:14.043(1 row(s) affected) CODO ERGO SUM |
 |
|
|
|
|
|
|