-2

I have a following sql query which run at:

PostgreSQL 9.5.1, compiled by Visual C++ build 1800, 64-bit

but not at:

PostgreSQL 8.2.15 (Greenplum Database 4.3.5.4 build 1) on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.4.2

SELECT id,
(SELECT AVG(dur)
    FROM data t
    WHERE t.id = t1.id AND 
        t.id IN (SELECT id 
                    FROM data t2
                    WHERE t2.id = t1.id
                    ORDER BY dur
                    DESC LIMIT 10)) as avgdur
FROM data t1 
WHERE t1.b<10000
ORDER BY avgdur 
DESC LIMIT 1;

I get the following error: ERROR: correlated subquery with skip-level correlations is not supported (subselect.c:394)

How should i modify the query?

2
  • see these link, it might help. stackoverflow.com/questions/426221/… Commented Apr 7, 2016 at 8:18
  • WHERE t2.id = t1.id)) as avgdur -->> WHERE t2.id = t.id)) as avgdur But the query can probably be simplified. Commented Apr 7, 2016 at 8:34

3 Answers 3

0

Your problems lies in WHERE t2.id = t1.id. You can use in a subquery the query at the parent level but not at upper levels. So here you have 3 levels:

  1. t1
  2. t
  3. t2

You can use t from the "t2 subquery" (query where t2 is in the FROM clause) and you can use t1 in your "t subquery". BUT you CANNOT use t1 from your "t2 subquery".

A fix could be to use WHERE t2.id = t.id)) as avgdur.

Some infos here : http://www.greenplumdba.com/correlated-subqueries-csqs-in-greenplum

Sign up to request clarification or add additional context in comments.

Comments

0

Looks to me as you're simply trying to do a group by in a different way...

SELECT id, AVG(dur) as avgdur
FROM data t1 
WHERE t1.b<10000
GROUP BY id
ORDER BY avgdur 
DESC LIMIT 1;

1 Comment

yes, but i have to do more things, i just simplified the question, i have to find the id where the 5 max element average is the maximum
0

Pivotal Query Optimizer (available in 4.3.5) supports queries with multi-level correlation. To enable Pivotal Query Optimizer, run the following commands.

set optimizer =on;

I would encourage you to upgrade to the latest GPDB version of 4.3.8 which has several improvements and bug fixes.

Let me know if this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.