~vkolesnikov/pbxt/pbxt-1.5-23.06.2011

« back to all changes in this revision

Viewing changes to test/mysql-test/test_data/505/r/having.result

  • Committer: vladimir at primebase
  • Date: 2011-06-23 15:11:13 UTC
  • mfrom: (824.1.48 pbxt11)
  • Revision ID: vladimir@primebase.org-20110623151113-ie7mezix8dv68jts
merged changes from PBXT 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
drop table if exists t1,t2,t3;
 
2
create table t1 (a int);
 
3
select count(a) as b from t1 where a=0 having b > 0;
 
4
b
 
5
insert into t1 values (null);
 
6
select count(a) as b from t1 where a=0 having b > 0;
 
7
b
 
8
select count(a) as b from t1 where a=0 having b >=0;
 
9
b
 
10
0
 
11
explain extended select count(a) as b from t1 where a=0 having b >=0;
 
12
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
13
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    1       100.00  Using where
 
14
Warnings:
 
15
Note    1003    select count(`test`.`t1`.`a`) AS `b` from `test`.`t1` where (`test`.`t1`.`a` = 0) having (`b` >= 0)
 
16
drop table t1;
 
17
CREATE TABLE t1 (
 
18
raw_id int(10) NOT NULL default '0',
 
19
chr_start int(10) NOT NULL default '0',
 
20
chr_end int(10) NOT NULL default '0',
 
21
raw_start int(10) NOT NULL default '0',
 
22
raw_end int(10) NOT NULL default '0',
 
23
raw_ori int(2) NOT NULL default '0'
 
24
);
 
25
INSERT INTO t1 VALUES (469713,1,164123,1,164123,1),(317330,164124,317193,101,153170,1),(469434,317194,375620,101,58527,1),(591816,375621,484273,1,108653,1),(591807,484274,534671,91,50488,1),(318885,534672,649362,101,114791,1),(318728,649363,775520,102,126259,1),(336829,775521,813997,101,38577,1),(317740,813998,953227,101,139330,1),(1,813998,953227,101,139330,1);
 
26
CREATE TABLE t2 (
 
27
id int(10) unsigned NOT NULL default '0',
 
28
contig_id int(10) unsigned NOT NULL default '0',
 
29
seq_start int(10) NOT NULL default '0',
 
30
seq_end int(10) NOT NULL default '0',
 
31
strand tinyint(2) NOT NULL default '0',
 
32
KEY id (id)
 
33
);
 
34
INSERT INTO t2 VALUES (133195,469713,61327,61384,1),(133196,469713,64113,64387,1),(133197,1,1,1,0),(133197,1,1,1,-2);
 
35
SELECT e.id,
 
36
MIN( IF(sgp.raw_ori=1,
 
37
(e.seq_start+sgp.chr_start-sgp.raw_start),  
 
38
(sgp.chr_start+sgp.raw_end-e.seq_end))) as start, 
 
39
MAX( IF(sgp.raw_ori=1,
 
40
(e.seq_end+sgp.chr_start-sgp.raw_start),  
 
41
(sgp.chr_start+sgp.raw_end-e.seq_start))) as end, 
 
42
AVG(IF (sgp.raw_ori=1,e.strand,(-e.strand))) as chr_strand 
 
43
FROM  t1 sgp,
 
44
t2 e  
 
45
WHERE sgp.raw_id=e.contig_id 
 
46
GROUP BY e.id 
 
47
HAVING chr_strand= -1 and end >= 0 
 
48
AND start <= 999660;
 
49
id      start   end     chr_strand
 
50
133197  813898  813898  -1.0000
 
51
drop table t1,t2;
 
52
CREATE TABLE t1 (Fld1 int(11) default NULL,Fld2 int(11) default NULL);
 
53
INSERT INTO t1 VALUES (1,10),(1,20),(2,NULL),(2,NULL),(3,50);
 
54
select Fld1, max(Fld2) as q from t1 group by Fld1 having q is not null;
 
55
Fld1    q
 
56
1       20
 
57
3       50
 
58
select Fld1, max(Fld2) from t1 group by Fld1 having max(Fld2) is not null;
 
59
Fld1    max(Fld2)
 
60
1       20
 
61
3       50
 
62
select Fld1, max(Fld2) from t1 group by Fld1 having avg(Fld2) is not null;
 
63
Fld1    max(Fld2)
 
64
1       20
 
65
3       50
 
66
select Fld1, max(Fld2) from t1 group by Fld1 having std(Fld2) is not null;
 
67
Fld1    max(Fld2)
 
68
1       20
 
69
3       50
 
70
select Fld1, max(Fld2) from t1 group by Fld1 having variance(Fld2) is not null;
 
71
Fld1    max(Fld2)
 
72
1       20
 
73
3       50
 
74
drop table t1;
 
75
create table t1 (id int not null, qty int not null);
 
76
insert into t1 values (1,2),(1,3),(2,4),(2,5);
 
77
select id, sum(qty) as sqty from t1 group by id having sqty>2;
 
78
id      sqty
 
79
1       5
 
80
2       9
 
81
select sum(qty) as sqty from t1 group by id having count(id) > 0;
 
82
sqty
 
83
5
 
84
9
 
85
select sum(qty) as sqty from t1 group by id having count(distinct id) > 0;
 
86
sqty
 
87
5
 
88
9
 
89
drop table t1;
 
90
CREATE TABLE t1 (
 
91
`id` bigint(20) NOT NULL default '0',
 
92
`description` text
 
93
) ENGINE=MyISAM;
 
94
CREATE TABLE t2 (
 
95
`id` bigint(20) NOT NULL default '0',
 
96
`description` varchar(20)
 
97
) ENGINE=MyISAM;
 
98
INSERT INTO t1  VALUES (1, 'test');
 
99
INSERT INTO t2 VALUES (1, 'test');
 
100
CREATE TABLE t3 (
 
101
`id`       bigint(20) NOT NULL default '0',
 
102
`order_id` bigint(20) NOT NULL default '0'
 
103
) ENGINE=MyISAM;
 
104
select
 
105
a.id, a.description,
 
106
count(b.id) as c 
 
107
from t1 a left join t3 b on a.id=b.order_id 
 
108
group by a.id, a.description 
 
109
having (a.description is not null) and (c=0);
 
110
id      description     c
 
111
1       test    0
 
112
select
 
113
a.*, 
 
114
count(b.id) as c 
 
115
from t2 a left join t3 b on a.id=b.order_id 
 
116
group by a.id, a.description
 
117
having (a.description is not null) and (c=0);
 
118
id      description     c
 
119
1       test    0
 
120
INSERT INTO t1  VALUES (2, 'test2');
 
121
select
 
122
a.id, a.description,
 
123
count(b.id) as c 
 
124
from t1 a left join t3 b on a.id=b.order_id 
 
125
group by a.id, a.description 
 
126
having (a.description is not null) and (c=0);
 
127
id      description     c
 
128
1       test    0
 
129
2       test2   0
 
130
drop table t1,t2,t3;
 
131
CREATE TABLE t1 (a int);
 
132
INSERT INTO t1 VALUES (3), (4), (1), (3), (1);
 
133
SELECT SUM(a) FROM t1 GROUP BY a HAVING SUM(a)>0;
 
134
SUM(a)
 
135
2
 
136
6
 
137
4
 
138
SELECT SUM(a) FROM t1 GROUP BY a HAVING SUM(a);
 
139
SUM(a)
 
140
2
 
141
6
 
142
4
 
143
DROP TABLE t1;
 
144
CREATE TABLE t1 (a int);
 
145
INSERT INTO t1 VALUES (1), (2), (1), (3), (2), (1);
 
146
SELECT a FROM t1 GROUP BY a HAVING a > 1;
 
147
a
 
148
2
 
149
3
 
150
SELECT a FROM t1 GROUP BY a HAVING 1 != 1 AND a > 1;
 
151
a
 
152
SELECT 0 AS x, a FROM t1 GROUP BY x,a HAVING x=1 AND a > 1;
 
153
x       a
 
154
EXPLAIN SELECT a FROM t1 GROUP BY a HAVING 1 != 1 AND a > 1;
 
155
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
156
1       SIMPLE  NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible HAVING
 
157
EXPLAIN SELECT 0 AS x, a FROM t1 GROUP BY x,a HAVING x=1 AND a > 1;
 
158
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
159
1       SIMPLE  NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible HAVING
 
160
DROP table t1;
 
161
create table t1 (col1 int, col2 varchar(5), col_t1 int);
 
162
create table t2 (col1 int, col2 varchar(5), col_t2 int);
 
163
create table t3 (col1 int, col2 varchar(5), col_t3 int);
 
164
insert into t1 values(10,'hello',10);
 
165
insert into t1 values(20,'hello',20);
 
166
insert into t1 values(30,'hello',30);
 
167
insert into t1 values(10,'bye',10);
 
168
insert into t1 values(10,'sam',10);
 
169
insert into t1 values(10,'bob',10);
 
170
insert into t2 select * from t1;
 
171
insert into t3 select * from t1;
 
172
select count(*) from t1 group by col1 having col1 = 10;
 
173
count(*)
 
174
4
 
175
select count(*) as count_col1 from t1 group by col1 having col1 = 10;
 
176
count_col1
 
177
4
 
178
select count(*) as count_col1 from t1 as tmp1 group by col1 having col1 = 10;
 
179
count_col1
 
180
4
 
181
select count(*) from t1 group by col2 having col2 = 'hello';
 
182
count(*)
 
183
3
 
184
select count(*) from t1 group by col2 having col1 = 10;
 
185
ERROR 42S22: Unknown column 'col1' in 'having clause'
 
186
select col1 as count_col1 from t1 as tmp1 group by col1 having col1 = 10;
 
187
count_col1
 
188
10
 
189
select col1 as count_col1 from t1 as tmp1 group by col1 having count_col1 = 10;
 
190
count_col1
 
191
10
 
192
select col1 as count_col1 from t1 as tmp1 group by count_col1 having col1 = 10;
 
193
count_col1
 
194
10
 
195
select col1 as count_col1 from t1 as tmp1 group by count_col1 having count_col1 = 10;
 
196
count_col1
 
197
10
 
198
select col1 as count_col1,col2 from t1 as tmp1 group by col1,col2 having col1 = 10;
 
199
count_col1      col2
 
200
10      bob
 
201
10      bye
 
202
10      hello
 
203
10      sam
 
204
select col1 as count_col1,col2 from t1 as tmp1 group by col1,col2 having count_col1 = 10;
 
205
count_col1      col2
 
206
10      bob
 
207
10      bye
 
208
10      hello
 
209
10      sam
 
210
select col1 as count_col1,col2 from t1 as tmp1 group by col1,col2 having col2 = 'hello';
 
211
count_col1      col2
 
212
10      hello
 
213
20      hello
 
214
30      hello
 
215
select col1 as count_col1,col2 as group_col2 from t1 as tmp1 group by col1,col2 having group_col2 = 'hello';
 
216
count_col1      group_col2
 
217
10      hello
 
218
20      hello
 
219
30      hello
 
220
select sum(col1) as co12 from t1 group by col2 having col2 10;
 
221
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '10' at line 1
 
222
select sum(col1) as co2, count(col2) as cc from t1 group by col1 having col1 =10;
 
223
co2     cc
 
224
40      4
 
225
select t2.col2 from t2 group by t2.col1, t2.col2 having t1.col1 <= 10;
 
226
ERROR 42S22: Unknown column 't1.col1' in 'having clause'
 
227
select t1.col1 from t1
 
228
where t1.col2 in 
 
229
(select t2.col2 from t2 
 
230
group by t2.col1, t2.col2 having t2.col1 <= 10);
 
231
col1
 
232
10
 
233
20
 
234
30
 
235
10
 
236
10
 
237
10
 
238
select t1.col1 from t1
 
239
where t1.col2 in 
 
240
(select t2.col2 from t2
 
241
group by t2.col1, t2.col2
 
242
having t2.col1 <=
 
243
(select min(t3.col1) from t3));
 
244
col1
 
245
10
 
246
20
 
247
30
 
248
10
 
249
10
 
250
10
 
251
select t1.col1 from t1
 
252
where t1.col2 in
 
253
(select t2.col2 from t2 
 
254
group by t2.col1, t2.col2 having t1.col1 <= 10);
 
255
col1
 
256
10
 
257
10
 
258
10
 
259
10
 
260
select t1.col1 as tmp_col from t1
 
261
where t1.col2 in 
 
262
(select t2.col2 from t2 
 
263
group by t2.col1, t2.col2 having tmp_col <= 10);
 
264
tmp_col
 
265
10
 
266
10
 
267
10
 
268
10
 
269
select t1.col1 from t1
 
270
where t1.col2 in 
 
271
(select t2.col2 from t2 
 
272
group by t2.col1, t2.col2 having col_t1 <= 10);
 
273
col1
 
274
10
 
275
10
 
276
10
 
277
10
 
278
select sum(col1) from t1
 
279
group by col_t1
 
280
having (select col_t1 from t2 where col_t1 = col_t2 order by col_t2 limit 1);
 
281
sum(col1)
 
282
40
 
283
20
 
284
30
 
285
select t1.col1 from t1
 
286
where t1.col2 in 
 
287
(select t2.col2 from t2 
 
288
group by t2.col1, t2.col2 having col_t1 <= 10)
 
289
having col_t1 <= 20;
 
290
ERROR 42S22: Unknown column 'col_t1' in 'having clause'
 
291
select t1.col1 from t1
 
292
where t1.col2 in 
 
293
(select t2.col2 from t2 
 
294
group by t2.col1, t2.col2 having col_t1 <= 10)
 
295
group by col_t1
 
296
having col_t1 <= 20;
 
297
col1
 
298
10
 
299
select col_t1, sum(col1) from t1
 
300
group by col_t1
 
301
having col_t1 > 10 and
 
302
exists (select sum(t2.col1) from t2
 
303
group by t2.col2 having t2.col2 > 'b');
 
304
col_t1  sum(col1)
 
305
20      20
 
306
30      30
 
307
select sum(col1) from t1
 
308
group by col_t1
 
309
having col_t1 in (select sum(t2.col1) from t2
 
310
group by t2.col2, t2.col1 having t2.col1 = t1.col1);
 
311
ERROR 42S22: Unknown column 't1.col1' in 'having clause'
 
312
select sum(col1) from t1
 
313
group by col_t1
 
314
having col_t1 in (select sum(t2.col1) from t2
 
315
group by t2.col2, t2.col1 having t2.col1 = col_t1);
 
316
sum(col1)
 
317
40
 
318
20
 
319
30
 
320
select t1.col1, t2.col1 from t1, t2 where t1.col1 = t2.col1
 
321
group by t1.col1, t2.col1 having col1 = 2;
 
322
ERROR 23000: Column 'col1' in having clause is ambiguous
 
323
select t1.col1*10+t2.col1 from t1,t2 where t1.col1=t2.col1
 
324
group by t1.col1, t2.col1 having col1 = 2;
 
325
ERROR 23000: Column 'col1' in having clause is ambiguous
 
326
drop table t1, t2, t3;
 
327
create table t1 (s1 int);
 
328
insert into t1 values (1),(2),(3);
 
329
select count(*) from t1 group by s1 having s1 is null;
 
330
count(*)
 
331
select s1*0 as s1 from t1 group by s1 having s1 <> 0;
 
332
s1
 
333
0
 
334
0
 
335
0
 
336
Warnings:
 
337
Warning 1052    Column 's1' in group statement is ambiguous
 
338
Warning 1052    Column 's1' in having clause is ambiguous
 
339
select s1*0 from t1 group by s1 having s1 = 0;
 
340
s1*0
 
341
select s1 from t1 group by 1 having 1 = 0;
 
342
s1
 
343
select count(s1) from t1 group by s1 having count(1+1)=2;
 
344
count(s1)
 
345
select count(s1) from t1 group by s1 having s1*0=0;
 
346
count(s1)
 
347
1
 
348
1
 
349
1
 
350
select * from t1 a, t1 b group by a.s1 having s1 is null;
 
351
ERROR 23000: Column 's1' in having clause is ambiguous
 
352
drop table t1;
 
353
create table t1 (s1 char character set latin1 collate latin1_german1_ci);
 
354
insert into t1 values ('ü'),('y');
 
355
Warnings:
 
356
Warning 1265    Data truncated for column 's1' at row 1
 
357
select s1,count(s1) from t1
 
358
group by s1 collate latin1_swedish_ci having s1 = 'y';
 
359
s1      count(s1)
 
360
y       1
 
361
drop table t1;
 
362
DROP SCHEMA IF EXISTS HU;
 
363
CREATE SCHEMA HU ;
 
364
USE HU ;
 
365
CREATE TABLE STAFF
 
366
(EMPNUM   CHAR(3) NOT NULL UNIQUE,
 
367
EMPNAME  CHAR(20),
 
368
GRADE    DECIMAL(4),
 
369
CITY     CHAR(15));
 
370
CREATE TABLE PROJ
 
371
(PNUM     CHAR(3) NOT NULL UNIQUE,
 
372
PNAME    CHAR(20),
 
373
PTYPE    CHAR(6),
 
374
BUDGET   DECIMAL(9),
 
375
CITY     CHAR(15));
 
376
INSERT INTO STAFF VALUES ('E1','Alice',12,'Deale');
 
377
INSERT INTO STAFF VALUES ('E2','Betty',10,'Vienna');
 
378
INSERT INTO STAFF VALUES ('E3','Carmen',13,'Vienna');
 
379
INSERT INTO STAFF VALUES ('E4','Don',12,'Deale');
 
380
INSERT INTO STAFF VALUES ('E5','Ed',13,'Akron');
 
381
INSERT INTO PROJ VALUES  ('P1','MXSS','Design',10000,'Deale');
 
382
INSERT INTO PROJ VALUES  ('P2','CALM','Code',30000,'Vienna');
 
383
INSERT INTO PROJ VALUES  ('P3','SDP','Test',30000,'Tampa');
 
384
INSERT INTO PROJ VALUES  ('P4','SDP','Design',20000,'Deale');
 
385
INSERT INTO PROJ VALUES  ('P5','IRM','Test',10000,'Vienna');
 
386
INSERT INTO PROJ VALUES  ('P6','PAYR','Design',50000,'Deale');
 
387
SELECT EMPNUM, GRADE*1000
 
388
FROM HU.STAFF WHERE GRADE * 1000 > 
 
389
ANY (SELECT SUM(BUDGET) FROM HU.PROJ
 
390
GROUP BY CITY, PTYPE
 
391
HAVING HU.PROJ.CITY = HU.STAFF.CITY);
 
392
EMPNUM  GRADE*1000
 
393
E3      13000
 
394
DROP SCHEMA HU;
 
395
USE test;
 
396
create table t1(f1 int);
 
397
select f1 from t1 having max(f1)=f1;
 
398
f1
 
399
select f1 from t1 group by f1 having max(f1)=f1;
 
400
f1
 
401
set session sql_mode='ONLY_FULL_GROUP_BY';
 
402
select f1 from t1 having max(f1)=f1;
 
403
ERROR 42000: non-grouping field 'f1' is used in HAVING clause
 
404
select f1 from t1 group by f1 having max(f1)=f1;
 
405
f1
 
406
set session sql_mode='';
 
407
drop table t1;