blob: 7052c392cbb77f41230e292584aef0e7e13136e9 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drhda932812000-06-06 18:00:15 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drhda932812000-06-06 18:00:15 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
drhda932812000-06-06 18:00:15 +00009#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing aggregate functions and the
13# GROUP BY and HAVING clauses of SELECT statements.
14#
drhb19a2bc2001-09-16 00:13:26 +000015# $Id: select3.test,v 1.3 2001/09/16 00:13:28 drh Exp $
drhda932812000-06-06 18:00:15 +000016
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Build some test data
21#
22do_test select3-1.0 {
23 set fd [open data1.txt w]
24 for {set i 1} {$i<32} {incr i} {
25 for {set j 0} {pow(2,$j)<$i} {incr j} {}
26 puts $fd "$i\t$j"
27 }
28 close $fd
29 execsql {
30 CREATE TABLE t1(n int, log int);
31 COPY t1 FROM 'data1.txt'
32 }
33 file delete data1.txt
34 execsql {SELECT DISTINCT log FROM t1 ORDER BY log}
35} {0 1 2 3 4 5}
36
37# Basic aggregate functions.
38#
39do_test select3-1.1 {
40 execsql {SELECT count(*) FROM t1}
41} {31}
42do_test select3-1.2 {
43 execsql {
44 SELECT min(n),min(log),max(n),max(log),sum(n),sum(log),avg(n),avg(log)
45 FROM t1
46 }
47} {1 0 31 5 496 124 16 4}
48do_test select3-1.3 {
49 execsql {SELECT max(n)/avg(n), max(log)/avg(log) FROM t1}
50} {1.9375 1.25}
51
52# Try some basic GROUP BY clauses
53#
54do_test select3-2.1 {
55 execsql {SELECT log, count(*) FROM t1 GROUP BY log ORDER BY log}
56} {0 1 1 1 2 2 3 4 4 8 5 15}
57do_test select3-2.2 {
58 execsql {SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log}
59} {0 1 1 2 2 3 3 5 4 9 5 17}
60do_test select3-2.3 {
61 execsql {SELECT log, avg(n) FROM t1 GROUP BY log ORDER BY log}
62} {0 1 1 2 2 3.5 3 6.5 4 12.5 5 24}
63do_test select3-2.3 {
64 execsql {SELECT log, avg(n)+1 FROM t1 GROUP BY log ORDER BY log}
65} {0 2 1 3 2 4.5 3 7.5 4 13.5 5 25}
66do_test select3-2.4 {
67 execsql {SELECT log, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
68} {0 0 1 0 2 0.5 3 1.5 4 3.5 5 7}
69do_test select3-2.5 {
70 execsql {SELECT log*2+1, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
71} {1 0 3 0 5 0.5 7 1.5 9 3.5 11 7}
72
73# Cannot have a HAVING without a GROUP BY
74#
75do_test select3-3.1 {
76 set v [catch {execsql {SELECT log, count(*) FROM t1 HAVING log>=4}} msg]
77 lappend v $msg
78} {1 {a GROUP BY clause is required before HAVING}}
79
80# Toss in some HAVING clauses
81#
82do_test select3-4.1 {
83 execsql {SELECT log, count(*) FROM t1 GROUP BY log HAVING log>=4 ORDER BY log}
84} {4 8 5 15}
85do_test select3-4.2 {
86 execsql {
87 SELECT log, count(*) FROM t1
88 GROUP BY log
89 HAVING count(*)>=4
90 ORDER BY log
91 }
92} {3 4 4 8 5 15}
93do_test select3-4.3 {
94 execsql {
95 SELECT log, count(*) FROM t1
96 GROUP BY log
97 HAVING count(*)>=4
98 ORDER BY max(n)
99 }
100} {3 4 4 8 5 15}
101
drh4cfa7932000-06-08 15:10:46 +0000102do_test select3-5.1 {
103 execsql {
104 SELECT log, count(*), avg(n), max(n+log*2) FROM t1
105 GROUP BY log
106 ORDER BY max(n+log*2), avg(n)
107 }
108} {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}
109do_test select3-5.2 {
110 execsql {
111 SELECT log, count(*), avg(n), max(n+log*2) FROM t1
112 GROUP BY log
113 ORDER BY max(n+log*2), min(log,avg(n))
114 }
115} {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}
116
drhda932812000-06-06 18:00:15 +0000117finish_test