Jump to content

Average count over last n seconds


sphinx9999

Recommended Posts

Hi,

 

I'm creating a query that will calculate the average count over the last n (60) seconds. I have got the first part:

 

select count(*) as counter from logquick group by logtime order by logtime desc limit 60;

 

This brings back something like this:

 

counter
8
4
15
6
5
2
2
10
...
7
2
4
6
6
6
2
2

 

Any ideas how I can now add in the averaging segment? Currently I am doing this in the PHP but obviously doing it all in one query would be better.

 

select avg(count(*))as counter from logquick group by logtime order by logtime desc limit 60;

1111 - Invalid use of group function

 

Thanx

Link to comment
Share on other sites

That looked gr8 but unfortunately gives 1248 - Every derived table must have its own alias. I assume this must mean that the outer select must be named...

 

Update: Just added 'as t' and its working fine now! Full query is

select avg(counter) from (
  select count(*) as counter from logquick group by logtime order by logtime desc limit 60
) as t;

 

Multi-thanx for your help fnairb

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.