blob: f77eae4b487fd16a31c509e3b49c817d7093fa11 [file] [log] [blame]
drhda932812000-06-06 18:00:15 +00001# Copyright (c) 1999, 2000 D. Richard Hipp
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public
5# License as published by the Free Software Foundation; either
6# version 2 of the License, or (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11# General Public License for more details.
12#
13# You should have received a copy of the GNU General Public
14# License along with this library; if not, write to the
15# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16# Boston, MA 02111-1307, USA.
17#
18# Author contact information:
19# drh@hwaci.com
20# http://www.hwaci.com/drh/
21#
22#***********************************************************************
23# This file implements regression tests for SQLite library. The
24# focus of this file is testing aggregate functions and the
25# GROUP BY and HAVING clauses of SELECT statements.
26#
drh4cfa7932000-06-08 15:10:46 +000027# $Id: select3.test,v 1.2 2000/06/08 15:10:48 drh Exp $
drhda932812000-06-06 18:00:15 +000028
29set testdir [file dirname $argv0]
30source $testdir/tester.tcl
31
32# Build some test data
33#
34do_test select3-1.0 {
35 set fd [open data1.txt w]
36 for {set i 1} {$i<32} {incr i} {
37 for {set j 0} {pow(2,$j)<$i} {incr j} {}
38 puts $fd "$i\t$j"
39 }
40 close $fd
41 execsql {
42 CREATE TABLE t1(n int, log int);
43 COPY t1 FROM 'data1.txt'
44 }
45 file delete data1.txt
46 execsql {SELECT DISTINCT log FROM t1 ORDER BY log}
47} {0 1 2 3 4 5}
48
49# Basic aggregate functions.
50#
51do_test select3-1.1 {
52 execsql {SELECT count(*) FROM t1}
53} {31}
54do_test select3-1.2 {
55 execsql {
56 SELECT min(n),min(log),max(n),max(log),sum(n),sum(log),avg(n),avg(log)
57 FROM t1
58 }
59} {1 0 31 5 496 124 16 4}
60do_test select3-1.3 {
61 execsql {SELECT max(n)/avg(n), max(log)/avg(log) FROM t1}
62} {1.9375 1.25}
63
64# Try some basic GROUP BY clauses
65#
66do_test select3-2.1 {
67 execsql {SELECT log, count(*) FROM t1 GROUP BY log ORDER BY log}
68} {0 1 1 1 2 2 3 4 4 8 5 15}
69do_test select3-2.2 {
70 execsql {SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log}
71} {0 1 1 2 2 3 3 5 4 9 5 17}
72do_test select3-2.3 {
73 execsql {SELECT log, avg(n) FROM t1 GROUP BY log ORDER BY log}
74} {0 1 1 2 2 3.5 3 6.5 4 12.5 5 24}
75do_test select3-2.3 {
76 execsql {SELECT log, avg(n)+1 FROM t1 GROUP BY log ORDER BY log}
77} {0 2 1 3 2 4.5 3 7.5 4 13.5 5 25}
78do_test select3-2.4 {
79 execsql {SELECT log, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
80} {0 0 1 0 2 0.5 3 1.5 4 3.5 5 7}
81do_test select3-2.5 {
82 execsql {SELECT log*2+1, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
83} {1 0 3 0 5 0.5 7 1.5 9 3.5 11 7}
84
85# Cannot have a HAVING without a GROUP BY
86#
87do_test select3-3.1 {
88 set v [catch {execsql {SELECT log, count(*) FROM t1 HAVING log>=4}} msg]
89 lappend v $msg
90} {1 {a GROUP BY clause is required before HAVING}}
91
92# Toss in some HAVING clauses
93#
94do_test select3-4.1 {
95 execsql {SELECT log, count(*) FROM t1 GROUP BY log HAVING log>=4 ORDER BY log}
96} {4 8 5 15}
97do_test select3-4.2 {
98 execsql {
99 SELECT log, count(*) FROM t1
100 GROUP BY log
101 HAVING count(*)>=4
102 ORDER BY log
103 }
104} {3 4 4 8 5 15}
105do_test select3-4.3 {
106 execsql {
107 SELECT log, count(*) FROM t1
108 GROUP BY log
109 HAVING count(*)>=4
110 ORDER BY max(n)
111 }
112} {3 4 4 8 5 15}
113
drh4cfa7932000-06-08 15:10:46 +0000114do_test select3-5.1 {
115 execsql {
116 SELECT log, count(*), avg(n), max(n+log*2) FROM t1
117 GROUP BY log
118 ORDER BY max(n+log*2), avg(n)
119 }
120} {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}
121do_test select3-5.2 {
122 execsql {
123 SELECT log, count(*), avg(n), max(n+log*2) FROM t1
124 GROUP BY log
125 ORDER BY max(n+log*2), min(log,avg(n))
126 }
127} {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}
128
drhda932812000-06-06 18:00:15 +0000129finish_test