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 2012 Forums
 Transact-SQL (2012)
 Get record count by time intervals

Author  Topic 

mattluk
Starting Member

15 Posts

Posted - 2013-09-29 : 22:29:19
Our DB server stored a lot of job records.
Such as:
JobID   StartTime    EndTime
1000001 1/1/1900 17:01 1/1/1900 17:11
1000002 1/1/1900 17:11 1/1/1900 17:12
1000003 1/1/1900 17:12 1/1/1900 17:21
1000004 1/1/1900 17:13 1/1/1900 17:31
1000005 1/1/1900 17:20 1/1/1900 17:25


I would like to count by evey 10 mins.

The result like this
JobID   StartTime    EndTime
17:00 1
17:10 4
17:20 3
17:30 1
17:40 0


Anyone can give me a hand?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2013-09-30 : 01:34:28
[code]SELECT dateadd(minute, datediff(minute, 0, StartTime)/10*10, 0), COUNT(*)
FROM . . .
GROUP BY dateadd(minute, datediff(minute, 0, StartTime)/10*10, 0)[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2013-09-30 : 07:53:10
http://thefirstsql.com/2010/06/23/finding-the-most-active-15-minute-time-period/

- Lumbago
My blog-> http://thefirstsql.com
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-09-30 : 08:39:12
StartTime and EndTime columns should be changed to TIME datatype.

ALTER TABLE dbo.Table1 ALTER COLUMN StartTime TIME(0) NOT NULL
ALTER TABLE dbo.Table1 ALTER COLUMN EndTime TIME(0) NOT NULL




Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-09-30 : 09:56:11
see

http://visakhm.blogspot.in/2010/02/aggregating-data-over-time-slots.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -