drop table colors if exists;
drop table sizes if exists;
drop table fruits if exists;
drop table trees if exists;
create table colors(id int, val char(20));
insert into colors values(1,'red');
insert into colors values(2,'green');
insert into colors values(3,'orange');
insert into colors values(4,'indigo');
create table sizes(id int, val char(20));
insert into sizes values(1,'small');
insert into sizes values(2,'medium');
insert into sizes values(3,'large');
insert into sizes values(4,'odd');
create table fruits(id int, name char(30), color_id int);
insert into fruits values(1, 'golden delicious',2);
insert into fruits values(2, 'macintosh',1);
insert into fruits values(3, 'red delicious',1);
insert into fruits values(4, 'granny smith',2);
insert into fruits values(5, 'tangerine',4);
create table trees(id int, name char(30), fruit_id int, size_id int);
insert into trees values(1, 'small golden delicious tree',1,1);
insert into trees values(2, 'large macintosh tree',2,3);
insert into trees values(3, 'large red delicious tree',3,3);
insert into trees values(4, 'small red delicious tree',3,1);
insert into trees values(5, 'medium granny smith tree',4,2);
select a.val, b.name from sizes a, trees b where a.id = b.size_id and b.id in (select a.id from trees a, fruits b where a.fruit_id = b.id and b.name='red delicious') order by a.val;
--
-- bug #547764
-- name in subquery must resolve to the subquery first
drop table trees if exists;
drop table fruits if exists;
create table trees(id integer primary key,name varchar(30) not null);
create table fruits(id integer primary key,name varchar(30) not null,
 tree_id integer not null,foreign key (tree_id) references trees(id));
insert into trees (id, name) values (1, 'apple');
insert into fruits (id, name, tree_id) values(1, 'pippin', 1);
insert into fruits (id, name, tree_id) values(2, 'granny smith', 1);
/*c2*/select id from fruits where tree_id in(select id from trees where name = 'apple');

drop table table1 if exists;
drop table table2 if exists;
CREATE TABLE TABLE1 (COL1 INTEGER, COL2 CHAR(1))
CREATE TABLE TABLE2 (COL1 INTEGER)
insert into table1 values 1, 'X'
insert into table2 values 1
/*r1*/SELECT T1.COL1 FROM TABLE1 T1 INNER JOIN
 (SELECT COL1 FROM TABLE2) T2 ON T1.COL1 = T2.COL1
 WHERE T1.COL2 = 'X'

/*r
 null,4
 5,6
*/select * from (
 select null as aaaaaaa, 4 as b from table2
 union select 5 as aaaaaaa, 6 as b from table2
 ) baz

drop table table1
drop table table2

-- mixed aggregate and subquery with aggregate with group by
CREATE CACHED TABLE PUBLIC.VC_CORE_TERMINALPROFILE_TAG(TERMINALPROFILE_ID
  BIGINT NOT NULL,
  TAG_ID BIGINT NOT NULL,
  PRIMARY KEY(TERMINALPROFILE_ID,
  TAG_ID));
CREATE CACHED TABLE PUBLIC.VC_DEVICE_TERMINAL(ID BIGINT NOT NULL PRIMARY KEY,
  VERSION INTEGER NOT NULL,
  NAME VARCHAR(64),
  NOTES VARCHAR(1024),
  ACTIVE BIT,
  STATE INTEGER NOT NULL,
  TERMINALPROFILE_ID BIGINT );
CREATE CACHED TABLE PUBLIC.VC_DEVICE_TERMINALPROFILE(ID BIGINT GENERATED BY
  DEFAULT AS IDENTITY(START WITH 1) NOT NULL PRIMARY KEY,
  NAME VARCHAR(128),
  DATECREATED TIMESTAMP,
  DATECREATEDTIMEZONEID VARCHAR(50),
  FAKEDOUTFFISIZE BIGINT,
  FAKEDOUTFFIMD5 VARCHAR(255),
  TEMPORARYPROFILE BIT,
  NETWORKCONFIGURATION_ID BIGINT,
  FFIFILE_ID BIGINT,
  BBIFILE_ID BIGINT,
  RRIFILE_ID BIGINT,
  FIRMWARE_ID BIGINT);
CREATE CACHED TABLE PUBLIC.VOC_SITE(SITEID BIGINT GENERATED BY DEFAULT AS
  IDENTITY(START WITH 1) NOT NULL PRIMARY KEY,
  VERSION INTEGER NOT NULL,
  NAME VARCHAR(255) NOT NULL,
  DESCRIPTION VARCHAR(2000),
  NOTES VARCHAR(2000),
  TIMEZONE VARCHAR(255),
  EAPTYPE INTEGER,
  CREDENTIALASSOCIATION INTEGER,
  CREDENTIALTYPE INTEGER,
  USEPINS BIT,
  CHARGERDISCONNECT BIT,
  RESTRICTEDSSID VARCHAR(128),
  RESTRICTEDEAPTYPE INTEGER,
  SITEPIN VARCHAR(32),
  SERVERCREDENTIALS_ID BIGINT,
  SITEWIDEUSER_ID BIGINT,
  RESTRICTEDUSER_ID BIGINT,
  LDAPCONFIG_ID BIGINT,
  UNIQUE(NAME)) ;
CREATE CACHED TABLE PUBLIC.VOC_TAG(ID BIGINT GENERATED BY DEFAULT AS
  IDENTITY(START WITH 1) NOT NULL PRIMARY KEY,
  TAG_TYPE BIGINT,
  TAGGABLE_ID BIGINT);
CREATE INDEX IDX_TERMINAL_TERMINALPROFILE
    ON PUBLIC.VC_DEVICE_TERMINAL(TERMINALPROFILE_ID);
ALTER TABLE PUBLIC.VOC_SITE ALTER COLUMN SITEID RESTART WITH 1;
ALTER TABLE PUBLIC.VOC_TAG ALTER COLUMN ID RESTART WITH 1;
ALTER TABLE PUBLIC.VC_CORE_TERMINALPROFILE_TAG ADD CONSTRAINT
  FKEC676CBA9924AE56 FOREIGN KEY(TERMINALPROFILE_ID) REFERENCES
  PUBLIC.VC_DEVICE_TERMINALPROFILE(ID);
ALTER TABLE PUBLIC.VC_CORE_TERMINALPROFILE_TAG ADD CONSTRAINT
  FKEC676CBA3EF30942 FOREIGN KEY(TAG_ID) REFERENCES PUBLIC.VOC_TAG(ID);
ALTER TABLE PUBLIC.VC_DEVICE_TERMINAL ADD CONSTRAINT
  FK_TERMINAL_TERMINALPROFILE FOREIGN KEY(TERMINALPROFILE_ID) REFERENCES
  PUBLIC.VC_DEVICE_TERMINALPROFILE(ID);

INSERT INTO VC_DEVICE_TERMINALPROFILE (id, temporaryProfile, name)
  VALUES (1, 0, 'tp1');
INSERT INTO VC_DEVICE_TERMINALPROFILE (id, temporaryProfile, name)
  VALUES (2, 0, 'tp2');
INSERT INTO voc_tag (id, tag_type, taggable_id)
  VALUES (-463, 1, -463);
INSERT INTO voc_tag (id, tag_type, taggable_id)
  VALUES (-25, 1, -24);
INSERT INTO voc_tag (id, tag_type, taggable_id)
  VALUES (-1, 1, -1);
INSERT INTO vc_core_terminalprofile_tag (terminalprofile_id, tag_id)
  VALUES (1, -1);
INSERT INTO vc_core_terminalprofile_tag (terminalprofile_id, tag_id)
  VALUES (2,   -1);
INSERT INTO voc_site (siteid, version, name, description)
  VALUES (-24, 0, 'two', 'second');
INSERT INTO voc_site (siteid, version, name, description)
  VALUES (-1, 0, 'one', 'first');
SELECT terminalpr1_.id as termid, terminalpr1_.name, min(terminalpr1_.name) as name,
  count(terminals2_.id) as terminals,
  (SELECT count(*) FROM vc_device_terminal terminal5_
  WHERE terminal5_.terminalprofile_id=terminalpr1_.id  AND terminal5_.state=0) as col_3_0_,
  (SELECT count(*) FROM vc_device_terminal terminal6_
  WHERE terminal6_.terminalprofile_id=terminalpr1_.id AND (terminal6_.state IN (1 , 6))) as col_4_0_,
  (SELECT count(*) FROM vc_device_terminal terminal7_ WHERE
  terminal7_.terminalprofile_id=terminalpr1_.id AND terminal7_.state=3) as col_5_0_,
  (SELECT count(*) FROM vc_device_terminal terminal8_ WHERE
  terminal8_.terminalprofile_id=terminalpr1_.id AND terminal8_.state=4) as col_6_0_,
  (SELECT count(*) FROM vc_device_terminal terminal9_
  WHERE terminal9_.terminalprofile_id=terminalpr1_.id AND terminal9_.state=5) as col_7_0_,
  (SELECT count(*) FROM vc_device_terminal terminal10_
  WHERE terminal10_.terminalprofile_id=terminalpr1_.id AND terminal10_.state=7) as col_8_0_,
  (SELECT count(*)
  FROM vc_device_terminal terminal11_
  WHERE terminal11_.terminalprofile_id=terminalpr1_.id AND terminal11_.state=8) as col_9_0_,
  (SELECT count(*) FROM vc_device_terminal terminal12_
  WHERE terminal12_.terminalprofile_id=terminalpr1_.id AND terminal12_.state=-1) as col_10_0_,
  max(site0_.name) as col_11_0_,
  max(site0_.siteId) as col_12_0_
  FROM voc_site site0_,
  vc_core_terminalprofile_tag tags3_,
  voc_tag tag4_,
  vc_device_terminalprofile terminalpr1_
    LEFT OUTER JOIN vc_device_terminal terminals2_
    ON terminalpr1_.id=terminals2_.terminalprofile_id
    AND (terminals2_.active=1)
  WHERE terminalpr1_.temporaryProfile=0
    AND terminalpr1_.id=tags3_.terminalprofile_id
    AND tags3_.tag_id=tag4_.id
    AND tag4_.taggable_id=site0_.siteId
    AND (tag4_.id IN (-1))
  GROUP BY terminalpr1_.id;

