# 障害票 1362
# 明示的JOINとGROUP BYの組み合わせ

Begin;
Initialize;
# テスト用のDBを作る
InitializeSession "";
Command "create database TESTDB";
TerminateSession;

InitializeSession "TESTDB";

Command  "create table TBL1 (f int, g int, primary key(f))";
Command  "create index IDX_TBL1_g on TBL1(g)";

Command  "create table TBL2 (x int, y int, primary key(x))";

Command  "insert TBL1 values (1,10), (2,20), (3,30)";
Command  "insert TBL1 select f+3, g from TBL1";

Command  "insert TBL2 values (1,100), (2,200), (3,300)";
Command  "insert TBL2 select x+3, y from TBL2";
Command  "insert TBL2 select x+6, y from TBL2";
Command  "insert TBL2 select x+12, y from TBL2";
Command  "insert TBL2 select x+24, y from TBL2";
Command  "insert TBL2 select x+48, y from TBL2";

#--落ちる
Command  "select TBL1.f from TBL1 JOIN TBL2 on TBL1.g = TBL2.x group by (TBL1.f)";
Command  "select TBL1.f, count(TBL2.y) from TBL1 join TBL2 on TBL1.g = TBL2.x where TBL1.f = 3 group by TBL1.f";
Command  "select TBL1.f, count(TBL2.y) from TBL1 left join TBL2 on TBL1.g = TBL2.x where TBL1.f = 3 group by TBL1.f";
Command  "select TBL1.g, count(TBL2.y) from TBL1 join TBL2 on TBL1.g = TBL2.x where TBL1.f = 3 group by TBL1.g";
Command  "select TBL1.g, count(TBL2.y), count(TBL2.y) from TBL1 join TBL2 on TBL1.g = TBL2.x where TBL1.f = 3 group by TBL1.g";
#--Bad argument
Command  "select TBL1.f, count(TBL2.y), count(TBL2.x) from TBL1 join TBL2 on TBL1.g = TBL2.x where TBL1.f = 3 or TBL2.x = 2 group by TBL1.f";

#--大丈夫
Command  "select TBL1.f, count(TBL1.f) from TBL1 where TBL1.g = 20 group by TBL1.f";
Command  "select TBL1.f, count(TBL2.y) from TBL1 , TBL2 where TBL1.g = TBL2.x and TBL1.f = 3 group by TBL1.f";
Command  "select TBL1.f, count(TBL2.y) from TBL1 right join TBL2 on TBL1.g = TBL2.x where TBL1.f = 3 group by TBL1.f";
Command  "select TBL1.f, TBL2.x from TBL1 , TBL2 where TBL2.x = 2 order by TBL1.f";
Command  "select TBL1.g, TBL2.x, TBL2.y from TBL1 join TBL2 on TBL1.g = TBL2.x where TBL1.f = 3 order by TBL1.g";

Command  "drop table TBL1";
Command  "drop table TBL2";

TerminateSession;
# DBの後始末
InitializeSession "";
Command "drop database TESTDB";
TerminateSession;
Terminate;
End;