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: select5.test,v 1.6 2001/10/15 00:44:36 drh Exp $
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
23 # Build some test data
25 set fd [open data1.txt w]
26 for {set i 1} {$i<32} {incr i} {
27 for {set j 0} {pow(2,$j)<$i} {incr j} {}
28 puts $fd "[expr {32-$i}]\t[expr {10-$j}]"
32 CREATE TABLE t1(x int, y int);
33 COPY t1 FROM 'data1.txt'
38 execsql {SELECT DISTINCT y FROM t1 ORDER BY y}
41 # Sort by an aggregate function.
44 execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY y}
45 } {5 15 6 8 7 4 8 2 9 1 10 1}
47 execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY count(*), y}
48 } {9 1 10 1 8 2 7 4 6 8 5 15}
50 execsql {SELECT count(*), y FROM t1 GROUP BY y ORDER BY count(*), y}
51 } {1 9 1 10 2 8 4 7 8 6 15 5}
53 # Some error messages associated with aggregates and GROUP BY
56 set v [catch {execsql {
57 SELECT y, count(*) FROM t1 GROUP BY z ORDER BY y
60 } {1 {no such column: z}}
62 set v [catch {execsql {
63 SELECT y, count(*) FROM t1 GROUP BY z(y) ORDER BY y
66 } {1 {no such function: z}}
68 set v [catch {execsql {
69 SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<3 ORDER BY y
74 set v [catch {execsql {
75 SELECT y, count(*) FROM t1 GROUP BY y HAVING z(y)<3 ORDER BY y
78 } {1 {no such function: z}}
80 set v [catch {execsql {
81 SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<z ORDER BY y
84 } {1 {no such column: z}}
86 # Get the Agg function to rehash in vdbe.c
90 SELECT x, count(*), avg(y) FROM t1 GROUP BY x HAVING x<4 ORDER BY x
94 # Run various aggregate functions when the count is zero.
98 SELECT avg(x) FROM t1 WHERE x>100
101 do_test select5-4.2 {
103 SELECT count(x) FROM t1 WHERE x>100
106 do_test select5-4.3 {
108 SELECT min(x) FROM t1 WHERE x>100
111 do_test select5-4.4 {
113 SELECT max(x) FROM t1 WHERE x>100
116 do_test select5-4.5 {
118 SELECT sum(x) FROM t1 WHERE x>100