Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temp table doesn't exist error using nested aggregation

Tags:

sql

mysql

I'm trying to use nested aggregation for one of my queries:

SELECT t.type, t.avgrent
FROM (  SELECT e.type, AVG(e.rental_rate) AS avgrent
        FROM Equipment e
        GROUP BY e.type) AS t
WHERE t.avgrent IN (SELECT MIN(t.avgrent) FROM t)

And I keep getting the following error:

DB query error: Table 'database.t' doesn't exist

Can anyone tell me what I'm doing wrong?

Thanks!

like image 617
Sadiq Avatar asked Jul 18 '26 01:07

Sadiq


1 Answers

You can not reuse T like that. Something like this maybe:

SELECT t.type, t.avgrent
FROM (  SELECT e.type, AVG(e.rental_rate) AS avgrent
        FROM Equipment e
        GROUP BY e.type) AS t
WHERE
    t.avgrent IN
(
    SELECT
        MIN(t2.avgrent)
    FROM
    (
        SELECT 
           e.type, 
           AVG(e.rental_rate) AS avgrent
        FROM Equipment e
        GROUP BY e.type
    ) AS t2
)
like image 63
Arion Avatar answered Jul 19 '26 14:07

Arion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!