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 |
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2012-06-24 : 00:33:18
|
How to calculate Avg, Min, Max, Standard Deviation for each person with multiple timedate values in a row. Only interested in 00:06:31 format as the resultSome intervals maybe NULL.Select Name, [Interval 1], [Interval 2], [Interval 3], [Interval 4], [Interval 5], [Interval 5]From IntervalName Iterval 1 Iterval 2 Iterval 3 Iterval 4 Iterval 5 Iterval 5Kim Mottrom 2012-06-24 00:06:39.000 2012-06-24 00:06:34.000 2012-06-24 00:06:33.000 2012-06-24 00:06:31.000 2012-06-24 00:06:28.000 2012-06-24 00:06:28.000Justin Hill 2012-06-24 00:07:07.000 2012-06-24 00:07:12.000 2012-06-24 00:07:19.000 2012-06-24 00:07:19.000 2012-06-24 00:07:13.000 2012-06-24 00:07:13.000Megan Szirom NULL NULL NULL NULL NULL NULLPeter Crump 2012-06-24 00:07:41.000 2012-06-24 00:07:49.000 2012-06-24 00:07:48.000 2012-06-24 00:07:51.000 2012-06-24 00:07:44.000 2012-06-24 00:07:44.000Bill Starr 2012-06-24 00:10:08.000 2012-06-24 00:10:11.000 2012-06-24 00:10:04.000 2012-06-24 00:10:15.000 2012-06-24 00:10:16.000 2012-06-24 00:10:16.000Jim Hoare 2012-06-24 00:10:48.000 2012-06-24 00:10:58.000 2012-06-24 00:10:07.000 2012-06-24 00:10:07.000 2012-06-24 00:10:11.000 2012-06-24 00:10:11.000Rhiannon Lovegrove 2012-06-24 00:08:18.000 NULL 2012-06-24 00:08:28.000 NULL NULL NULLAmber Tuschorski 2012-06-24 00:08:21.000 2012-06-24 00:08:21.000 2012-06-24 00:08:26.000 2012-06-24 00:08:28.000 2012-06-24 00:08:43.000 2012-06-24 00:08:43.000Alix Harlington 2012-06-24 00:07:26.000 NULL 2012-06-24 00:07:38.000 NULL 2012-06-24 00:07:32.000 2012-06-24 00:07:32.000Corey Harlington 2012-06-24 00:12:19.000 NULL 2012-06-24 00:11:41.000 NULL NULL NULLDanielle Walsh 2012-06-24 00:08:09.000 NULL 2012-06-24 00:08:22.000 NULL NULL NULLRhiana Hooker 2012-06-24 00:08:38.000 NULL 2012-06-24 00:09:31.000 NULL 2012-06-24 00:09:20.000 2012-06-24 00:09:20.000Ayeisha Wallace 2012-06-24 00:09:57.000 NULL 2012-06-24 00:09:59.000 NULL NULL NULLTroy Hooker 2012-06-24 00:11:05.000 2012-06-24 00:11:30.000 2012-06-24 00:11:29.000 2012-06-24 00:11:37.000 2012-06-24 00:11:39.000 2012-06-24 00:11:39.000Anna Cross 2012-06-24 00:07:26.000 2012-06-24 00:07:44.000 NULL 2012-06-24 00:07:59.000 2012-06-24 00:07:54.000 2012-06-24 00:07:54.000Beth Cross 2012-06-24 00:08:55.000 NULL 2012-06-24 00:08:40.000 2012-06-24 00:09:35.000 NULL NULLSummer Pelenstov 2012-06-24 00:12:12.000 NULL 2012-06-24 00:11:41.000 NULL NULL NULLGloria Holliday 2012-06-24 00:08:05.000 2012-06-24 00:08:06.000 2012-06-24 00:08:08.000 2012-06-24 00:08:03.000 2012-06-24 00:08:22.000 2012-06-24 00:08:22.000Lachlan Tyler-Dowd 2012-06-24 00:08:09.000 2012-06-24 00:09:13.000 NULL 2012-06-24 00:08:03.000 2012-06-24 00:08:13.000 2012-06-24 00:08:13.000Michael Nicolaides 2012-06-24 00:09:41.000 2012-06-24 00:10:56.000 2012-06-24 00:11:32.000 NULL NULL NULLMark Hooker 2012-06-24 00:12:51.000 NULL 2012-06-24 00:13:16.000 NULL 2012-06-24 00:11:52.000 2012-06-24 00:11:52.000Ryan Hooker 2012-06-24 00:12:45.000 NULL 2012-06-24 00:12:24.000 NULL 2012-06-24 00:11:43.000 2012-06-24 00:11:43.000Riley Szirom 2012-06-24 00:12:45.000 NULL 2012-06-24 00:13:16.000 NULL NULL NULLChris Nicolaides 2012-06-24 00:10:43.000 2012-06-24 00:12:08.000 NULL NULL NULL NULL |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-06-24 : 00:38:10
|
[code]; with cte as( Select Name, Interval = datediff(second, 0, [Interval 1]) From Interval union all Select Name, Interval = datediff(second, 0, [Interval 2]) From Interval union all Select Name, Interval = datediff(second, 0, [Interval 3]) From Interval union all Select Name, Interval = datediff(second, 0, [Interval 4]) From Interval union all Select Name, Interval = datediff(second, 0, [Interval 5])From Interval)select Name, min_time = convert(varchar(10), dateadd(second, min(Interval), 0), 108), max_time = convert(varchar(10), dateadd(second, max(Interval), 0), 108), stddev_time = convert(varchar(10), dateadd(second, stddev(Interval), 0), 108)from ctegroup by Name[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2012-06-24 : 00:57:36
|
I get this error;Msg 535, Level 16, State 0, Line 8The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart. |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-06-25 : 02:05:31
|
try datediff(minute, ...) KH[spoiler]Time is always against us[/spoiler] |
 |
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2012-06-25 : 03:22:26
|
How would one add average time?avg_time = convert(varchar(10), dateadd(second, avg(Interval), 0), 108)Msg 8115, Level 16, State 2, Line 11Arithmetic overflow error converting expression to data type int. |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-06-25 : 03:26:23
|
are you working on the time only or do you need to take the date into consideration ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2012-06-25 : 03:29:49
|
time only thanks. |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-06-25 : 03:53:32
|
[code];withcte as( Select [Name], Interval = [Interval 1] From Interval union all Select [Name], Interval = [Interval 2] From Interval union all Select [Name], Interval = [Interval 3] From Interval union all Select [Name], Interval = [Interval 4] From Interval union all Select [Name], Interval = [Interval 5] From Interval),cte2 as( select [Name], Interval, min_time = min(Interval) over (partition by [Name]) from cte)select [Name], min_time = min(Interval), max_time = max(Interval), avg_time = dateadd(minute, avg(datediff(minute, min_time, Interval)), min_time), stddev_time = dateadd(minute, stdev(datediff(minute, min_time, Interval)), min_time)from cte2group by [Name], min_time[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2012-06-25 : 21:45:16
|
There seems to be an error for avg_time. You can see the time for Steve is 00:07:00.000 and it shoudl be 00:07:30.000Name Interval 1 Interval 2 Interval 3 Interval 4Steve 00:10:00.000 00:05:00.000 00:10:00.000 00:05:00.000Alix 00:07:26.000 00:07:38.000 NULL 00:07:59.000Heather NULL 00:08:00.000 NULL NULLCorey NULL 00:20:00.000 00:10:00.000 NULLName min_time max_time avg_time stddev_timeSteve 00:05:00.000 00:10:00.000 00:07:00.000 00:07:00.000Alix 00:07:26.000 00:07:59.000 00:07:26.000 00:07:26.000Heather 00:08:00.000 00:08:00.000 00:08:00.000 NULLCorey 00:10:00.000 00:20:00.000 00:15:00.000 00:17:00.000 |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-06-25 : 21:56:43
|
maybe this ?avg_time = dateadd(minute, avg(datediff(minute, min_time, Interval)), dateadd(day, datediff(day, 0, min_time), 0)) KH[spoiler]Time is always against us[/spoiler] |
 |
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2012-06-25 : 22:14:04
|
That gives 1900-01-01 00:02:00.000. |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-06-25 : 22:43:20
|
Does your data contains date and do you need to consider date at all ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2012-06-25 : 22:57:09
|
The input data is as follows; I could work with or without date but since these are race times date is not needed.Name Interval 1 Interval 2 Interval 3 Interval 4Steve 00:10:00.000 00:05:00.000 00:10:00.000 00:05:00.000Alix 00:07:26.000 00:07:38.000 NULL 00:07:59.000Heather NULL 00:08:00.000 NULL NULLCorey NULL 00:20:00.000 00:10:00.000 NULLJustin 06:31:00.100 06:31:00.000 08:10:06.000 10:11:04.500 |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-06-25 : 23:25:16
|
try;withcte as( Select [Name], Interval = dateadd(day, datediff(day, 0, [Interval 1]), 0) From Interval union all Select [Name], Interval = dateadd(day, datediff(day, 0, [Interval 2]), 0) From Interval union all Select [Name], Interval = dateadd(day, datediff(day, 0, [Interval 3]), 0) From Interval union all Select [Name], Interval = dateadd(day, datediff(day, 0, [Interval 4]), 0) From Interval union all Select [Name], Interval = dateadd(day, datediff(day, 0, [Interval 5]), 0) From Interval),,cte2 as( select [Name], Interval, min_time = min(Interval) over (partition by [Name]) from cte)select [Name], min_time = min(Interval), max_time = max(Interval), avg_time = dateadd(minute, avg(datediff(minute, 0, Interval)), 0, 0)), stdev_time = dateadd(minute, stdev(datediff(minute, min_time, Interval)), min_time)from cte2group by [Name], min_time KH[spoiler]Time is always against us[/spoiler] |
 |
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2012-06-26 : 00:29:07
|
Script above gives;Msg 174, Level 15, State 1, Line 64The dateadd function requires 3 argument(s).If I comment out the avg_time and, stdev_time I get;Name min_time max_timeAlix 1900-01-01 00:00:00.000 1900-01-01 00:00:00.000Corey 1900-01-01 00:00:00.000 1900-01-01 00:00:00.000Heather 1900-01-01 00:00:00.000 1900-01-01 00:00:00.000Justin 1900-01-01 00:00:00.000 1900-01-01 00:00:00.000Steve 1900-01-01 00:00:00.000 1900-01-01 00:00:00.000 |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-06-26 : 04:10:41
|
sorry, extra ")" thereavg_time = dateadd(minute, avg(datediff(minute, 0, Interval)), 0, 0), KH[spoiler]Time is always against us[/spoiler] |
 |
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2012-06-26 : 19:36:27
|
select [Name], min_time = min(Interval), max_time = max(Interval), avg_time = dateadd(minute, avg(datediff(minute, 0, Interval)), 0, 0) --stdev_time = dateadd(minute, stdev(datediff(minute, min_time, Interval)), min_time)from cte2Msg 174, Level 15, State 1, Line 64The dateadd function requires 3 argument(s). |
 |
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2012-06-26 : 19:39:37
|
select [Name], min_time = min(Interval), max_time = max(Interval), avg_time = dateadd(minute, avg(datediff(minute, 0, Interval)), 0) --stdev_time = dateadd(minute, stdev(datediff(minute, min_time, Interval)), min_time)from cte2Input Data;Name Interval 1 Interval 2 Interval 3 Interval 4Steve 00:10:00.000 00:05:00.000 00:10:00.000 00:05:00.000Alix 00:07:26.000 00:07:38.000 NULL 00:07:59.000Heather NULL 00:08:00.000 NULL NULLCorey NULL 00:20:00.000 00:10:00.000 NULLJustin 06:31:00.100 06:31:00.000 08:10:06.000 10:11:04.500Result;Name min_time max_time avg_timeAlix 1900-01-01 00:00:00.000 1900-01-01 00:00:00.000 1900-01-01 00:00:00.000Corey 1900-01-01 00:00:00.000 1900-01-01 00:00:00.000 1900-01-01 00:00:00.000Heather 1900-01-01 00:00:00.000 1900-01-01 00:00:00.000 1900-01-01 00:00:00.000Justin 1900-01-01 00:00:00.000 1900-01-01 00:00:00.000 1900-01-01 00:00:00.000Steve 1900-01-01 00:00:00.000 1900-01-01 00:00:00.000 1900-01-01 00:00:00.000 |
 |
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2012-06-27 : 19:22:35
|
Any other ideas, I still don't have a working solution for this problem?Interval datatype = time(3)Input Data;Name Interval 1 Interval 2 Interval 3 Interval 4Steve 00:10:00.000 00:05:00.000 00:10:00.000 00:05:00.000Alix 00:07:26.000 00:07:38.000 NULL 00:07:59.000Heather NULL 00:08:00.000 NULL NULLCorey NULL 00:20:00.000 00:10:00.000 NULLJustin 06:31:00.100 06:31:00.000 08:10:06.000 10:11:04.500For each person I need min time, max time, average time and standard deviation time. Note some races may have NULL values as the athlete did not compete in this race. Also for the same person they may do races with the same time. |
 |
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2012-07-03 : 21:31:53
|
Any other ideas, I still don't have a working solution for this problem? |
 |
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2012-07-03 : 23:46:48
|
Technically you're storing a duration (race time), not an actual time value, so neither datetime nor time is really optimal. Leaving it as is for now, the following should work:;WITH CTE AS(SELECT Name,DATEDIFF(s,0,CAST(TIME AS TIME)) Duration FROM(SELECT * FROM INTERVAL) aUNPIVOT(TIME FOR INTERVAL IN(Interval1,Interval2,Interval3,Interval4,Interval5,Interval6)) B)SELECT Name, AVG(Duration) AvgDuration, MAX(Duration) MaxDuration, MIN(Duration) MinDuration,STDEV(Duration) StdDevDuration FROM CTE GROUP BY Name I converted everything to time data type and then took the DATEDIFF in seconds, this avoids overflow and allows AVG and STDEV to be calculated. You can optionally do a DATEADD(second,#,0) to convert them back to datetime values but it may not make much sense (STDEV especially). |
 |
|
Next Page
|
|
|
|
|