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 |
|
mvanwyk
Yak Posting Veteran
99 Posts |
Posted - 2009-09-22 : 02:24:47
|
| Hi Guys.I have a strange one here so please bare with me.We have a ton of mobile units in the field.They connect to our server at a specific interval.I'm trying to figure out what that interval is.The data looks like this.mUnit Received Date123456 2009-09-22 07:54:15.973123111 2009-09-22 07:54:12.567111443 2009-09-22 07:54:11.960998776 2009-09-22 07:54:11.927665443 2009-09-22 07:54:10.990123456 2009-09-22 07:54:10.210123111 2009-09-22 07:54:09.270111443 2009-09-22 07:54:08.397998776 2009-09-22 07:54:07.583665443 2009-09-22 07:54:07.460123456 2009-09-22 07:54:07.300123111 2009-09-22 07:54:05.113111443 2009-09-22 07:54:04.880998776 2009-09-22 07:54:04.287665443 2009-09-22 07:54:02.613123456 2009-09-22 07:54:00.427123111 2009-09-22 07:53:59.567111443 2009-09-22 07:53:58.693You will notice from te datset above mUnit 123456 averages a new message every 5 sec.What query do i need to get that average interval for all mUnits.Hope this helps.Cheers. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-09-22 : 03:31:05
|
[code]DECLARE @Sample TABLE ( Unit INT, Received DATETIME )INSERT @SampleSELECT 123456, '20090922 07:54:15.973' UNION ALLSELECT 123111, '20090922 07:54:12.567' UNION ALLSELECT 111443, '20090922 07:54:11.960' UNION ALLSELECT 998776, '20090922 07:54:11.927' UNION ALLSELECT 665443, '20090922 07:54:10.990' UNION ALLSELECT 123456, '20090922 07:54:10.210' UNION ALLSELECT 123111, '20090922 07:54:09.270' UNION ALLSELECT 111443, '20090922 07:54:08.397' UNION ALLSELECT 998776, '20090922 07:54:07.583' UNION ALLSELECT 665443, '20090922 07:54:07.460' UNION ALLSELECT 123456, '20090922 07:54:07.300' UNION ALLSELECT 123111, '20090922 07:54:05.113' UNION ALLSELECT 111443, '20090922 07:54:04.880' UNION ALLSELECT 998776, '20090922 07:54:04.287' UNION ALLSELECT 665443, '20090922 07:54:02.613' UNION ALLSELECT 123456, '20090922 07:54:00.427' UNION ALLSELECT 123111, '20090922 07:53:59.567' UNION ALLSELECT 111443, '20090922 07:53:58.693'SELECT Unit, DATEDIFF(SECOND, MIN(Received), MAX(Received)) / (COUNT(*) - 1) AS avgIntervalFROM @SampleGROUP BY UnitORDER BY Unit[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-09-22 : 03:33:59
|
When I read the question earlier my first thought was: that's for Peso  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
mvanwyk
Yak Posting Veteran
99 Posts |
Posted - 2009-09-22 : 06:10:22
|
| Hi Peso...I changed it up a bit to use the correct fields...DECLARE @Sample TABLE ( UnitNumber varchar(50), createdate DATETIME )INSERT @SampleSELECT UnitNumber,createdate from [Message] SELECT UnitNumber, DATEDIFF(SECOND, MIN(createdate), MAX(createdate)) / (COUNT(*) - 1) AS avgIntervalFROM @SampleGROUP BY UnitNumberORDER BY UnitNumberBut get this error back...(733594 row(s) affected)Msg 8134, Level 16, State 1, Line 11Divide by zero error encountered. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-09-22 : 07:25:56
|
[code]SELECT UnitNumber, DATEDIFF(SECOND, MIN(CreateDate), MAX(CreateDate)) / (COALESCE(NULLIF(COUNT(*), 1), 2) - 1) AS avgIntervalFROM [Message]GROUP BY UnitNumberORDER BY UnitNumber[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
|
|
|
|
|
|