Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Count time of day at which records are created SQL

Posted on 2011-03-16
Medium Priority
268 Views
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
Question by:nhmedia
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 53

Accepted Solution

Dhaest earned 2000 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

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

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

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

ID: 35184272
0

## Featured Post

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
###### Suggested Courses
Course of the Month12 days, 2 hours left to enroll