Either both of the following should execute, or neither should.  And they 
should both produce the same error message if they are not executable.

SELECT max((select avg(x) FROM t2)) FROM t1;
SELECT max((SELECT x FROM t2 limit 1)) FROM t1;

In other words, either you can access columns from the outer query inside a 
correlated subquery, or you cannot.  Since the latter is acceptable but not the 
former, there is a bug.

Similarly, these two:

sqlite> SELECT max((SELECT avg(x+y) FROM t2)) FROM t1;
Error: misuse of aggregate: avg()
sqlite> SELECT max((SELECT x+y FROM t2)) FROM t1;

sqlite> insert into t1 values (1), (2), (3);
sqlite> insert into t2 values (1), (2), (3);
sqlite> SELECT max((SELECT x+y FROM t2)) FROM t1;
4
sqlite> SELECT max((SELECT avg(x+y) FROM t2)) FROM t1;
Error: misuse of aggregate: avg()

---
()  ascii ribbon campaign against html e-mail
/\  www.asciiribbon.org

> -----Original Message-----
> From: sqlite-users-boun...@sqlite.org [mailto:sqlite-users-
> boun...@sqlite.org] On Behalf Of Igor Tandetnik
> Sent: Thursday, 21 June, 2012 14:10
> To: sqlite-users@sqlite.org
> Subject: Re: [sqlite] Difference between 3.7.11 and 3.7.13
> 
> On 6/21/2012 3:52 PM, Pavel Ivanov wrote:
> >>>> CREATE TABLE t1(x);
> >>>> CREATE TABLE t2(y);
> >>>> SELECT max((SELECT avg(x) FROM t2)) FROM t1;
> >
> > So you are saying that behavior of such query should be equivalent to
> > "SELECT max(x) FROM t1"?
> 
> Not quite, but you get the idea. I think "select avg(SomeConstExpr) from
> t2" would return NULL rather than SomeConstExpr if t2 is empty. So it
> should be equivalent to
> 
> select max(case when (select count(*) from t2) == 0 then null else x
> end) from t1;
> --
> Igor Tandetnik
> 
> _______________________________________________
> sqlite-users mailing list
> sqlite-users@sqlite.org
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users



_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to