Solved

Count time of day at which records are created SQL

Posted on 2011-03-16
5
258 Views
Last Modified: 2012-05-11
I have a table where records are made throughout the day and night. I want to construct a query that will  return on average how many records are made at each hour.

How could I do this?

I have got as far as extracting the hour from the datetime field. But when I try and add a count/group the query breaks.

 select
      DATEPART(hour,DateTime) as 'Hour' from [mytable]
0
Comment
Question by:nhmedia
5 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 35145177
Did you try

select
      DATEPART(hour,DateTime) as 'Hour' , count(*)
from [mytable]
group by DATEPART(hour,DateTime)
0
 
LVL 5

Expert Comment

by:mayankagarwal
ID: 35145970
If you want the average the query can be:

select avg(select count(*)
from [mytable]
group by DATEPART(hour,DateTime)) from dual
0
 
LVL 10

Expert Comment

by:John Claes
ID: 35146004
The following is my suggestion

1) You need to Count the occurenced by Day and Hour
2) those Counts by the Hour schould then be used in your Average calculation



select Hour, AVG(HourSumming) as HourAverage
from
(
      select
            convert(nvarchar(10), date) as 'DATE' ,
            DATEPART(hour,date) as 'Hour' ,
            count(*) as HourSumming
      from  [mytable]
      group by       convert(nvarchar(10), date),
            DATEPART(hour,date)
) as CountedByHourAndDate
group by Hour

regards
poor beggar
0
 

Author Comment

by:nhmedia
ID: 35171231
Thanks very much for the suggestions...

I got the first solution to work! I was also interested in the average idea, but the second solution produced an error: avg function requires one argument.  

0
 

Author Closing Comment

by:nhmedia
ID: 35184272
Very helpful, worked straight away
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now