2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this file is testing aggregate functions and the
16 # GROUP BY and HAVING clauses of SELECT statements.
18 # $Id: select3.test,v 1.8 2003/01/31 17:16:37 drh Exp $
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
23 # Build some test data
26 set fd [open data1.txt w]
27 for {set i 1} {$i<32} {incr i} {
28 for {set j 0} {pow(2,$j)<$i} {incr j} {}
33 CREATE TABLE t1(n int, log int);
34 COPY t1 FROM 'data1.txt'
37 execsql {SELECT DISTINCT log FROM t1 ORDER BY log}
40 # Basic aggregate functions.
43 execsql {SELECT count(*) FROM t1}
47 SELECT min(n),min(log),max(n),max(log),sum(n),sum(log),avg(n),avg(log)
50 } {1 0 31 5 496 124 16 4}
52 execsql {SELECT max(n)/avg(n), max(log)/avg(log) FROM t1}
55 # Try some basic GROUP BY clauses
58 execsql {SELECT log, count(*) FROM t1 GROUP BY log ORDER BY log}
59 } {0 1 1 1 2 2 3 4 4 8 5 15}
61 execsql {SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log}
62 } {0 1 1 2 2 3 3 5 4 9 5 17}
64 execsql {SELECT log, avg(n) FROM t1 GROUP BY log ORDER BY log}
65 } {0 1 1 2 2 3.5 3 6.5 4 12.5 5 24}
67 execsql {SELECT log, avg(n)+1 FROM t1 GROUP BY log ORDER BY log}
68 } {0 2 1 3 2 4.5 3 7.5 4 13.5 5 25}
70 execsql {SELECT log, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
71 } {0 0 1 0 2 0.5 3 1.5 4 3.5 5 7}
73 execsql {SELECT log*2+1, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
74 } {1 0 3 0 5 0.5 7 1.5 9 3.5 11 7}
77 SELECT log*2+1 as x, count(*) FROM t1 GROUP BY x ORDER BY x
79 } {1 1 3 1 5 2 7 4 9 8 11 15}
82 SELECT log*2+1 AS x, count(*) AS y FROM t1 GROUP BY x ORDER BY y
84 } {1 1 3 1 5 2 7 4 9 8 11 15}
87 SELECT log*2+1 AS x, count(*) AS y FROM t1 GROUP BY x ORDER BY 10-(x+y)
89 } {11 15 9 8 7 4 5 2 3 1 1 1}
92 SELECT log, count(*) FROM t1 GROUP BY 'x' ORDER BY log;
94 } {1 {GROUP BY terms must not be non-integer constants}}
95 do_test select3-2.10 {
97 SELECT log, count(*) FROM t1 GROUP BY 0 ORDER BY log;
99 } {1 {GROUP BY column number 0 out of range - should be between 1 and 2}}
100 do_test select3-2.11 {
102 SELECT log, count(*) FROM t1 GROUP BY 3 ORDER BY log;
104 } {1 {GROUP BY column number 3 out of range - should be between 1 and 2}}
105 do_test select3-2.12 {
107 SELECT log, count(*) FROM t1 GROUP BY 1 ORDER BY log;
109 } {0 {0 1 1 1 2 2 3 4 4 8 5 15}}
110 #do_test select3-2.13 {
112 # SELECT log, count(*) FROM t1 GROUP BY 2 ORDER BY log;
114 #} {0 {0 1 1 1 2 2 3 4 4 8 5 15}}
115 #do_test select3-2.14 {
117 # SELECT log, count(*) FROM t1 GROUP BY count(*) ORDER BY log;
119 #} {0 {0 1 1 1 2 2 3 4 4 8 5 15}}
121 # Cannot have a HAVING without a GROUP BY
123 do_test select3-3.1 {
124 set v [catch {execsql {SELECT log, count(*) FROM t1 HAVING log>=4}} msg]
126 } {1 {a GROUP BY clause is required before HAVING}}
128 # Toss in some HAVING clauses
130 do_test select3-4.1 {
131 execsql {SELECT log, count(*) FROM t1 GROUP BY log HAVING log>=4 ORDER BY log}
133 do_test select3-4.2 {
135 SELECT log, count(*) FROM t1
141 do_test select3-4.3 {
143 SELECT log, count(*) FROM t1
149 do_test select3-4.4 {
151 SELECT log AS x, count(*) AS y FROM t1
157 do_test select3-4.5 {
159 SELECT log AS x FROM t1
166 do_test select3-5.1 {
168 SELECT log, count(*), avg(n), max(n+log*2) FROM t1
170 ORDER BY max(n+log*2), avg(n)
172 } {0 1 1 1 1 1 2 4 2 2 3.5 8 3 4 6.5 14 4 8 12.5 24 5 15 24 41}
173 do_test select3-5.2 {
175 SELECT log, count(*), avg(n), max(n+log*2) FROM t1
177 ORDER BY max(n+log*2), min(log,avg(n))
179 } {0 1 1 1 1 1 2 4 2 2 3.5 8 3 4 6.5 14 4 8 12.5 24 5 15 24 41}
181 # Test sorting of GROUP BY results in the presence of an index
182 # on the GROUP BY column.
184 do_test select3-6.1 {
186 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log;
188 } {0 1 1 2 2 3 3 5 4 9 5 17}
189 do_test select3-6.2 {
191 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log DESC;
193 } {5 17 4 9 3 5 2 3 1 2 0 1}
194 do_test select3-6.3 {
196 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1;
198 } {0 1 1 2 2 3 3 5 4 9 5 17}
199 do_test select3-6.4 {
201 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1 DESC;
203 } {5 17 4 9 3 5 2 3 1 2 0 1}
204 do_test select3-6.5 {
206 CREATE INDEX i1 ON t1(log);
207 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log;
209 } {0 1 1 2 2 3 3 5 4 9 5 17}
210 do_test select3-6.6 {
212 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log DESC;
214 } {5 17 4 9 3 5 2 3 1 2 0 1}
215 do_test select3-6.7 {
217 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1;
219 } {0 1 1 2 2 3 3 5 4 9 5 17}
220 do_test select3-6.8 {
222 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1 DESC;
224 } {5 17 4 9 3 5 2 3 1 2 0 1}