# Bug report 2328
# ジョインにソートを伴う副問い合わせ

Begin;
Initialize;
InitializeSession "TESTDB";
Command "create database TESTDB";

Command "create table TBL1 (f int, g int)";
Command "create table TBL2 (x int, y int)";

Command "create index TBL1_f on TBL1(f)";
Command "create all rows index TBL1_g on TBL1(g)";
Command "create index TBL2_x on TBL2(x)";
Command "create index TBL2_y on TBL2(y)";

Command "insert TBL1 values (1, 2)";
Command "insert TBL1 select f+1, g*2 from TBL1";
Command "insert TBL1 select f+2, g*2 from TBL1";
Command "insert TBL1 select f+4, g*2 from TBL1";
Command "insert TBL1 select f+16, g*2 from TBL1";

Command "insert TBL2 values (1, 3)";
Command "insert TBL2 select x+2, y*3 from TBL2";
Command "insert TBL2 select x+4, y*3 from TBL2";
Command "insert TBL2 select x+8, y*3 from TBL2";
Command "insert TBL2 select x+16, y*3 from TBL2";
Command "insert TBL2 select x+32, y*3 from TBL2";

Command "select * from TBL1, TBL2 where f = x and exists (select count(*) from TBL2 where f = y and x > 100 group by y having count(x) >= 1) order by f";

Command "drop database TESTDB";
TerminateSession;
Terminate;
End;
