I'm re-asking this question in a simplified and expanded manner.
Consider these sql statements:
create table foo (id INT, score INT);
insert into foo values (106, 4);
insert into foo values (107, 3);
insert into foo values (106, 5);
insert into foo values (107, 5);
select T1.id, avg(T1.score) avg1
from foo T1
group by T1.id
having not exists (
select T2.id, avg(T2.score) avg2
from foo T2
group by T2.id
having avg2 > avg1);
Using sqlite, the select
statement returns:
id avg1
---------- ----------
106 4.5
107 4.0
and mysql returns:
+------+--------+
| id | avg1 |
+------+--------+
| 106 | 4.5000 |
+------+--------+
As far as I can tell, mysql's results are correct, and sqlite's are incorrect. I tried to cast to real
with sqlite as in the following but it returns two records still:
select T1.id, cast(avg(cast(T1.score as real)) as real) avg1
from foo T1
group by T1.id
having not exists (
select T2.id, cast(avg(cast(T2.score as real)) as real) avg2
from foo T2
group by T2.id
having avg2 > avg1);
Why does sqlite return two records?
Quick update:
I ran the statement against the latest sqlite version (3.7.11) and still get two records.
Another update:
I sent an email to [email protected] about the issue.
Myself, I've been playing with VDBE and found something interesting. I split the execution trace of each loop of not exists
(one for each avg group).
To have three avg groups, I used the following statements:
create table foo (id VARCHAR(1), score INT);
insert into foo values ('c', 1.5);
insert into foo values ('b', 5.0);
insert into foo values ('a', 4.0);
insert into foo values ('a', 5.0);
PRAGMA vdbe_listing = 1;
PRAGMA vdbe_trace=ON;
select avg(score) avg1
from foo
group by id
having not exists (
select avg(T2.score) avg2
from foo T2
group by T2.id
having avg2 > avg1);
We clearly see that somehow what should be r:4.5
has become i:5
:
I'm now trying to see why that is.
Final edit:
So I've been playing enough with the sqlite source code. I understand the beast much better now, although I'll let the original developer sort it out as he seems to already be doing it:
http://www.sqlite.org/src/info/430bb59d79
Interestingly, to me at least, it seems that the newer versions (some times after the version I'm using) supports inserting multiple records as used in a test case added in the aforementioned commit:
CREATE TABLE t34(x,y);
INSERT INTO t34 VALUES(106,4), (107,3), (106,5), (107,5);
avg2
andavg1
didn't exist. I replaced them withMAX(T2.score)
andMAX(T1.score)
and it gave the SQLite result. When I created the table withscore REAL
it gave the MySQL result. Perhaps your MySQL schema is different to sqlites? – Sidwohlas
as inavg(T2.score) as avg2
(two occurences)? – OverflyWHERE
,GROUP BY
orHAVING
. – SidwohlSQL error: no such function: exists
. Keywords that are working under mysql may not in other databases systems. I would personally use theIN
keyword for testing the results of your subquery – Burnsavg
returns areal
:select typeof(avg(score)) from foo;
returnsreal
. – Overflyavg
withsum
, which does not require division. – BatishEXISTS
except for table creation and so on and when reading #25429 I may think it is not the best keyword for what you are trying to do. – BurnsEXISTS
wasn't supported then...); in any case, I have nothing for or againstEXISTS
. – OverflyIN
clause or the avg1 var in aEXISTS
clause it returns the correct result... – Burns