~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

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

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

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  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE noticed after reading const tables
 
14
Warnings:
 
15
Note    1003    select count('0') AS "b" from "test"."t1" where 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 (a int PRIMARY KEY);
 
162
CREATE TABLE t2 (b int PRIMARY KEY, a int);
 
163
CREATE TABLE t3 (b int, flag int);
 
164
INSERT INTO t1 VALUES (1);
 
165
INSERT INTO t2 VALUES (1,1), (2,1), (3,1);
 
166
INSERT INTO t3(b,flag) VALUES (2, 1);
 
167
SELECT t1.a
 
168
FROM t1 INNER JOIN t2 ON t1.a=t2.a LEFT JOIN t3 ON t2.b=t3.b
 
169
GROUP BY t1.a, t2.b HAVING MAX(t3.flag)=0;
 
170
a
 
171
SELECT DISTINCT t1.a, MAX(t3.flag)
 
172
FROM t1 INNER JOIN t2 ON t1.a=t2.a LEFT JOIN t3 ON t2.b=t3.b
 
173
GROUP BY t1.a, t2.b HAVING MAX(t3.flag)=0;
 
174
a       MAX(t3.flag)
 
175
SELECT DISTINCT t1.a
 
176
FROM t1 INNER JOIN t2 ON t1.a=t2.a LEFT JOIN t3 ON t2.b=t3.b
 
177
GROUP BY t1.a, t2.b HAVING MAX(t3.flag)=0;
 
178
a
 
179
DROP TABLE t1,t2,t3;
 
180
create table t1 (col1 int, col2 varchar(5), col_t1 int);
 
181
create table t2 (col1 int, col2 varchar(5), col_t2 int);
 
182
create table t3 (col1 int, col2 varchar(5), col_t3 int);
 
183
insert into t1 values(10,'hello',10);
 
184
insert into t1 values(20,'hello',20);
 
185
insert into t1 values(30,'hello',30);
 
186
insert into t1 values(10,'bye',10);
 
187
insert into t1 values(10,'sam',10);
 
188
insert into t1 values(10,'bob',10);
 
189
insert into t2 select * from t1;
 
190
insert into t3 select * from t1;
 
191
select count(*) from t1 group by col1 having col1 = 10;
 
192
count(*)
 
193
4
 
194
select count(*) as count_col1 from t1 group by col1 having col1 = 10;
 
195
count_col1
 
196
4
 
197
select count(*) as count_col1 from t1 as tmp1 group by col1 having col1 = 10;
 
198
count_col1
 
199
4
 
200
select count(*) from t1 group by col2 having col2 = 'hello';
 
201
count(*)
 
202
3
 
203
select count(*) from t1 group by col2 having col1 = 10;
 
204
ERROR 42S22: Unknown column 'col1' in 'having clause'
 
205
select col1 as count_col1 from t1 as tmp1 group by col1 having col1 = 10;
 
206
count_col1
 
207
10
 
208
select col1 as count_col1 from t1 as tmp1 group by col1 having count_col1 = 10;
 
209
count_col1
 
210
10
 
211
select col1 as count_col1 from t1 as tmp1 group by count_col1 having col1 = 10;
 
212
count_col1
 
213
10
 
214
select col1 as count_col1 from t1 as tmp1 group by count_col1 having count_col1 = 10;
 
215
count_col1
 
216
10
 
217
select col1 as count_col1,col2 from t1 as tmp1 group by col1,col2 having col1 = 10;
 
218
count_col1      col2
 
219
10      bob
 
220
10      bye
 
221
10      hello
 
222
10      sam
 
223
select col1 as count_col1,col2 from t1 as tmp1 group by col1,col2 having count_col1 = 10;
 
224
count_col1      col2
 
225
10      bob
 
226
10      bye
 
227
10      hello
 
228
10      sam
 
229
select col1 as count_col1,col2 from t1 as tmp1 group by col1,col2 having col2 = 'hello';
 
230
count_col1      col2
 
231
10      hello
 
232
20      hello
 
233
30      hello
 
234
select col1 as count_col1,col2 as group_col2 from t1 as tmp1 group by col1,col2 having group_col2 = 'hello';
 
235
count_col1      group_col2
 
236
10      hello
 
237
20      hello
 
238
30      hello
 
239
select sum(col1) as co12 from t1 group by col2 having col2 10;
 
240
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
 
241
select sum(col1) as co2, count(col2) as cc from t1 group by col1 having col1 =10;
 
242
co2     cc
 
243
40      4
 
244
select t2.col2 from t2 group by t2.col1, t2.col2 having t1.col1 <= 10;
 
245
ERROR 42S22: Unknown column 't1.col1' in 'having clause'
 
246
select t1.col1 from t1
 
247
where t1.col2 in 
 
248
(select t2.col2 from t2 
 
249
group by t2.col1, t2.col2 having t2.col1 <= 10);
 
250
col1
 
251
10
 
252
20
 
253
30
 
254
10
 
255
10
 
256
10
 
257
select t1.col1 from t1
 
258
where t1.col2 in 
 
259
(select t2.col2 from t2
 
260
group by t2.col1, t2.col2
 
261
having t2.col1 <=
 
262
(select min(t3.col1) from t3));
 
263
col1
 
264
10
 
265
20
 
266
30
 
267
10
 
268
10
 
269
10
 
270
select t1.col1 from t1
 
271
where t1.col2 in
 
272
(select t2.col2 from t2 
 
273
group by t2.col1, t2.col2 having t1.col1 <= 10);
 
274
col1
 
275
10
 
276
10
 
277
10
 
278
10
 
279
select t1.col1 as tmp_col from t1
 
280
where t1.col2 in 
 
281
(select t2.col2 from t2 
 
282
group by t2.col1, t2.col2 having tmp_col <= 10);
 
283
tmp_col
 
284
10
 
285
10
 
286
10
 
287
10
 
288
select t1.col1 from t1
 
289
where t1.col2 in 
 
290
(select t2.col2 from t2 
 
291
group by t2.col1, t2.col2 having col_t1 <= 10);
 
292
col1
 
293
10
 
294
10
 
295
10
 
296
10
 
297
select sum(col1) from t1
 
298
group by col_t1
 
299
having (select col_t1 from t2 where col_t1 = col_t2 order by col_t2 limit 1);
 
300
sum(col1)
 
301
40
 
302
20
 
303
30
 
304
select t1.col1 from t1
 
305
where t1.col2 in 
 
306
(select t2.col2 from t2 
 
307
group by t2.col1, t2.col2 having col_t1 <= 10)
 
308
having col_t1 <= 20;
 
309
ERROR 42S22: Unknown column 'col_t1' in 'having clause'
 
310
select t1.col1 from t1
 
311
where t1.col2 in 
 
312
(select t2.col2 from t2 
 
313
group by t2.col1, t2.col2 having col_t1 <= 10)
 
314
group by col_t1
 
315
having col_t1 <= 20;
 
316
col1
 
317
10
 
318
select col_t1, sum(col1) from t1
 
319
group by col_t1
 
320
having col_t1 > 10 and
 
321
exists (select sum(t2.col1) from t2
 
322
group by t2.col2 having t2.col2 > 'b');
 
323
col_t1  sum(col1)
 
324
20      20
 
325
30      30
 
326
select sum(col1) from t1
 
327
group by col_t1
 
328
having col_t1 in (select sum(t2.col1) from t2
 
329
group by t2.col2, t2.col1 having t2.col1 = t1.col1);
 
330
ERROR 42S22: Unknown column 't1.col1' in 'having clause'
 
331
select sum(col1) from t1
 
332
group by col_t1
 
333
having col_t1 in (select sum(t2.col1) from t2
 
334
group by t2.col2, t2.col1 having t2.col1 = col_t1);
 
335
sum(col1)
 
336
40
 
337
20
 
338
30
 
339
select t1.col1, t2.col1 from t1, t2 where t1.col1 = t2.col1
 
340
group by t1.col1, t2.col1 having col1 = 2;
 
341
ERROR 23000: Column 'col1' in having clause is ambiguous
 
342
select t1.col1*10+t2.col1 from t1,t2 where t1.col1=t2.col1
 
343
group by t1.col1, t2.col1 having col1 = 2;
 
344
ERROR 23000: Column 'col1' in having clause is ambiguous
 
345
drop table t1, t2, t3;
 
346
create table t1 (s1 int);
 
347
insert into t1 values (1),(2),(3);
 
348
select count(*) from t1 group by s1 having s1 is null;
 
349
count(*)
 
350
select s1*0 as s1 from t1 group by s1 having s1 <> 0;
 
351
s1
 
352
0
 
353
0
 
354
0
 
355
Warnings:
 
356
Warning 1052    Column 's1' in group statement is ambiguous
 
357
Warning 1052    Column 's1' in having clause is ambiguous
 
358
select s1*0 from t1 group by s1 having s1 = 0;
 
359
s1*0
 
360
select s1 from t1 group by 1 having 1 = 0;
 
361
s1
 
362
select count(s1) from t1 group by s1 having count(1+1)=2;
 
363
count(s1)
 
364
select count(s1) from t1 group by s1 having s1*0=0;
 
365
count(s1)
 
366
1
 
367
1
 
368
1
 
369
select * from t1 a, t1 b group by a.s1 having s1 is null;
 
370
ERROR 23000: Column 's1' in having clause is ambiguous
 
371
drop table t1;
 
372
create table t1 (s1 char character set latin1 collate latin1_german1_ci);
 
373
insert into t1 values ('b'),('y');
 
374
select s1,count(s1) from t1
 
375
group by s1 collate latin1_swedish_ci having s1 = 'y';
 
376
s1      count(s1)
 
377
y       1
 
378
drop table t1;
 
379
DROP SCHEMA IF EXISTS HU;
 
380
CREATE SCHEMA HU ;
 
381
USE HU ;
 
382
CREATE TABLE STAFF
 
383
(EMPNUM   CHAR(3) NOT NULL UNIQUE,
 
384
EMPNAME  CHAR(20),
 
385
GRADE    DECIMAL(4),
 
386
CITY     CHAR(15));
 
387
CREATE TABLE PROJ
 
388
(PNUM     CHAR(3) NOT NULL UNIQUE,
 
389
PNAME    CHAR(20),
 
390
PTYPE    CHAR(6),
 
391
BUDGET   DECIMAL(9),
 
392
CITY     CHAR(15));
 
393
INSERT INTO STAFF VALUES ('E1','Alice',12,'Deale');
 
394
INSERT INTO STAFF VALUES ('E2','Betty',10,'Vienna');
 
395
INSERT INTO STAFF VALUES ('E3','Carmen',13,'Vienna');
 
396
INSERT INTO STAFF VALUES ('E4','Don',12,'Deale');
 
397
INSERT INTO STAFF VALUES ('E5','Ed',13,'Akron');
 
398
INSERT INTO PROJ VALUES  ('P1','MXSS','Design',10000,'Deale');
 
399
INSERT INTO PROJ VALUES  ('P2','CALM','Code',30000,'Vienna');
 
400
INSERT INTO PROJ VALUES  ('P3','SDP','Test',30000,'Tampa');
 
401
INSERT INTO PROJ VALUES  ('P4','SDP','Design',20000,'Deale');
 
402
INSERT INTO PROJ VALUES  ('P5','IRM','Test',10000,'Vienna');
 
403
INSERT INTO PROJ VALUES  ('P6','PAYR','Design',50000,'Deale');
 
404
SELECT EMPNUM, GRADE*1000
 
405
FROM HU.STAFF WHERE GRADE * 1000 > 
 
406
ANY (SELECT SUM(BUDGET) FROM HU.PROJ
 
407
GROUP BY CITY, PTYPE
 
408
HAVING HU.PROJ.CITY = HU.STAFF.CITY);
 
409
EMPNUM  GRADE*1000
 
410
E3      13000
 
411
DROP SCHEMA HU;
 
412
USE test;
 
413
create table t1(f1 int);
 
414
select f1 from t1 having max(f1)=f1;
 
415
f1
 
416
select f1 from t1 group by f1 having max(f1)=f1;
 
417
f1
 
418
select f1 from t1 having max(f1)=f1;
 
419
f1
 
420
select f1 from t1 group by f1 having max(f1)=f1;
 
421
f1
 
422
drop table t1;