Jrc
Jrc

Reputation: 395

mysql Getting Average of several Columns with limit

Im trying to get an average of the last 60 items from 3 Columns each but im getting an error:

#1248 - Every derived table must have its own alias

this is the query:

SELECT AVG( Sensor1 ) , AVG( Sensor2 ) , AVG( Sensor3 ) 
FROM (

    SELECT Sensor1, Sensor2, Sensor3
    FROM temperatur
    ORDER BY Zeit DESC 
    LIMIT 0 , 60
)

Im quite lost on the Alias I believe ..

Upvotes: 2

Views: 42

Answers (2)

Russ Clarke
Russ Clarke

Reputation: 17909

I'm guessing at the sytax but I believe you need something more like this:

SELECT AVG( T.Sensor1 ) , AVG( T.Sensor2 ) , AVG( T.Sensor3 ) FROM (

SELECT Sensor1, Sensor2, Sensor3
FROM temperatur
ORDER BY Zeit DESC 
LIMIT 0 , 60

) T

Upvotes: 2

Taryn
Taryn

Reputation: 247860

MySQL requires an alias on all derived tables and subqueries and you are missing the alias after the closing parentheses:

SELECT AVG( d.Sensor1 ) , AVG( d.Sensor2 ) , AVG( d.Sensor3 ) 
FROM 
(
    SELECT Sensor1, Sensor2, Sensor3
    FROM temperatur
    ORDER BY Zeit DESC 
    LIMIT 0 , 60
) d --- add this

Upvotes: 4

Related Questions