sqlite/test/select3.test
drh da93281e8d :-) (CVS 59)
FossilOrigin-Name: a8fa6719d55b43f8d194aecfcae5af42d17742af
2000-06-06 18:00:15 +00:00

115 lines
3.3 KiB
Plaintext

# Copyright (c) 1999, 2000 D. Richard Hipp
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
# Author contact information:
# drh@hwaci.com
# http://www.hwaci.com/drh/
#
#***********************************************************************
# This file implements regression tests for SQLite library. The
# focus of this file is testing aggregate functions and the
# GROUP BY and HAVING clauses of SELECT statements.
#
# $Id: select3.test,v 1.1 2000/06/06 18:00:16 drh Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
# Build some test data
#
do_test select3-1.0 {
set fd [open data1.txt w]
for {set i 1} {$i<32} {incr i} {
for {set j 0} {pow(2,$j)<$i} {incr j} {}
puts $fd "$i\t$j"
}
close $fd
execsql {
CREATE TABLE t1(n int, log int);
COPY t1 FROM 'data1.txt'
}
file delete data1.txt
execsql {SELECT DISTINCT log FROM t1 ORDER BY log}
} {0 1 2 3 4 5}
# Basic aggregate functions.
#
do_test select3-1.1 {
execsql {SELECT count(*) FROM t1}
} {31}
do_test select3-1.2 {
execsql {
SELECT min(n),min(log),max(n),max(log),sum(n),sum(log),avg(n),avg(log)
FROM t1
}
} {1 0 31 5 496 124 16 4}
do_test select3-1.3 {
execsql {SELECT max(n)/avg(n), max(log)/avg(log) FROM t1}
} {1.9375 1.25}
# Try some basic GROUP BY clauses
#
do_test select3-2.1 {
execsql {SELECT log, count(*) FROM t1 GROUP BY log ORDER BY log}
} {0 1 1 1 2 2 3 4 4 8 5 15}
do_test select3-2.2 {
execsql {SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log}
} {0 1 1 2 2 3 3 5 4 9 5 17}
do_test select3-2.3 {
execsql {SELECT log, avg(n) FROM t1 GROUP BY log ORDER BY log}
} {0 1 1 2 2 3.5 3 6.5 4 12.5 5 24}
do_test select3-2.3 {
execsql {SELECT log, avg(n)+1 FROM t1 GROUP BY log ORDER BY log}
} {0 2 1 3 2 4.5 3 7.5 4 13.5 5 25}
do_test select3-2.4 {
execsql {SELECT log, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
} {0 0 1 0 2 0.5 3 1.5 4 3.5 5 7}
do_test select3-2.5 {
execsql {SELECT log*2+1, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
} {1 0 3 0 5 0.5 7 1.5 9 3.5 11 7}
# Cannot have a HAVING without a GROUP BY
#
do_test select3-3.1 {
set v [catch {execsql {SELECT log, count(*) FROM t1 HAVING log>=4}} msg]
lappend v $msg
} {1 {a GROUP BY clause is required before HAVING}}
# Toss in some HAVING clauses
#
do_test select3-4.1 {
execsql {SELECT log, count(*) FROM t1 GROUP BY log HAVING log>=4 ORDER BY log}
} {4 8 5 15}
do_test select3-4.2 {
execsql {
SELECT log, count(*) FROM t1
GROUP BY log
HAVING count(*)>=4
ORDER BY log
}
} {3 4 4 8 5 15}
do_test select3-4.3 {
execsql {
SELECT log, count(*) FROM t1
GROUP BY log
HAVING count(*)>=4
ORDER BY max(n)
}
} {3 4 4 8 5 15}
finish_test