tags:

views:

352

answers:

4

This query is producing counts of logins per hour:

SELECT DATEADD(hour, DATEDIFF(hour, 0, EVENT_DATETIME), 0), 
       COUNT(*)
  FROM EVENTS_ALL_RPT_V1
 WHERE EVENT_NAME = 'Login' 
   AND EVENT_DATETIME >= CONVERT(DATETIME, '2010-03-17 00:00:00', 120) 
   AND EVENT_DATETIME <= CONVERT(DATETIME, '2010-03-24 00:00:00', 120)
GROUP BY DATEADD(hour, DATEDIFF(hour, 0, EVENT_DATETIME), 0)
ORDER BY DATEADD(hour, DATEDIFF(hour, 0, EVENT_DATETIME), 0)

...with lots of results like this:

Datetime                 COUNT(*)
----------------------------------
2010-03-17 12:00:00.000  135
2010-03-17 13:00:00.000  129
2010-03-17 14:00:00.000  147

What I need to figure out is how to query the average logins per hour for a given day. Any help?

A: 

You can do something like this

SELECT AVG(cnt) FROM (
   your other query
) 

But you need to name the second column cnt and maybe add a where clause that only looks at the given date.

klausbyskov
That will give an average of all the hours returned, not per hour like the OP asks.
OMG Ponies
A: 

This will select all the logins on the 18th of March per hour and then calculates the average.

SELECT AVG(count) FROM
(
    SELECT COUNT(*) count
    FROM EVENTS_ALL_RPT_V1
    WHERE EVENT_NAME = 'Login' AND
    EVENT_DATETIME >= CONVERT(DATETIME,'2010-03-18 00:00:00', 120) AND
    EVENT_DATETIME <= CONVERT(DATETIME,'2010-03-19 00:00:00', 120)
    GROUP BY DATEADD(hour, DATEDIFF(hour, 0, EVENT_DATETIME), 0)
    ORDER BY DATEADD(hour, DATEDIFF(hour, 0, EVENT_DATETIME), 0)
)
Fabian
That will give an average of all the hours returned, not per hour like the OP asks.
OMG Ponies
He already has the query to get the average login count per hour for every hour on a given day, he just set it to a week.
Fabian
+2  A: 

Use the AVG aggregate function:

  SELECT CONCAT(DATE_FORMAT(t.event_datetime, '%Y-%m-%d %H'), ':00:00.000') AS hr,
         COUNT(*) AS cnt,
         AVG(*) AS avg
    FROM EVENTS_ALL_RPT_V1 t
   WHERE t.event_name = 'Login'
     AND t.event_datetime BETWEEN '2010-03-24' AND '2010-03-17'
GROUP BY CONCAT(DATE_FORMAT(t.event_datetime, '%Y-%m-%d %H'), ':00:00.000')
ORDER BY hr

The ORDER BY clause can use column aliases, but GROUP BY can not.

OMG Ponies
Thank you for the reply! Trying this I get: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' t.event_datetime), 0) AS hr, COUNT(*) AS cnt, AVG(*) AS avg' at line 1
jerrygarciuh
A: 

For March 17th, 2010:

SELECT COUNT(*) / 24
  FROM EVENTS_ALL_RPT_V1
 WHERE EVENT_NAME = 'Login' 
   AND EVENT_DATETIME >= CONVERT(DATETIME, '2010-03-17 00:00:00', 120) 
   AND EVENT_DATETIME < CONVERT(DATETIME, '2010-03-18 00:00:00', 120)
RedFilter
That's assuming that people are logging in on every hour of the day.
Fabian
@Fabian: No, it is not assuming they log in every hour. Rather, it is not *disregarding* hours where they do not login. The question title is Daily Average, not Average For Hours Where Users Logged In - my answer reflects my understanding of what the OP is asking for.
RedFilter