Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

COUNT every 5 minutes on a Date_Time field

Status
Not open for further replies.

eyalkz

Programmer
Dec 6, 2002
3
IL
I'm trying to write SQL query on SQL Server,
I need to count number of different requests (on a requests field)
every 5 minutes
The 5 minutes interval is supposed to be calculated on a Date_Time field
that contains years,months,days,hours,minutes,seconds...
Thanks
Eyal
 
Try this. Change the column and table names.

Select
Interval=(datepart(minute, yourcolumn) / 5) + 1,
Cnt=count(*)
From YourTable
Group By (datepart(minute, yourcolumn) / 5) + 1 Terry L. Broadbent - DBA
SQL Server Page:
If you want to get the best answer for your question read faq183-874.
 
I think this will get you what you want:

create table #test (col1 varchar(25), event datetime)

insert into #test values ('1','1/1/2003 12:05')
insert into #test values ('1','1/1/2003 12:07')
insert into #test values ('1','1/1/2003 12:15')
insert into #test values ('1','1/1/2003 12:08')
insert into #test values ('1','1/1/2003 12:35')
insert into #test values ('1','1/1/2003 12:45')
insert into #test values ('1','1/1/2003 12:22')
insert into #test values ('1','1/1/2003 12:32')
insert into #test values ('1','1/1/2003 14:32')

select col1,datepart(hh,event) TheHour,(datepart(mi,event)/5 ) * 5 TheMinute, count(*)
from #test
group by col1,datepart(hh,event),(datepart(mi,event)/5 ) * 5
order by col1,datepart(hh,event),(datepart(mi,event)/5 ) * 5

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top