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
  • Learn & ask questions
Solved

Count time of day at which records are created SQL

Posted on 2011-03-16
5
260 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

860 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