select3.test 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # 2001 September 15
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  6. # 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.
  9. #
  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. #
  15. # $Id: select3.test,v 1.5 2002/01/22 14:11:30 drh Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Build some test data
  19. #
  20. do_test select3-1.0 {
  21. set fd [open data1.txt w]
  22. for {set i 1} {$i<32} {incr i} {
  23. for {set j 0} {pow(2,$j)<$i} {incr j} {}
  24. puts $fd "$i\t$j"
  25. }
  26. close $fd
  27. execsql {
  28. CREATE TABLE t1(n int, log int);
  29. COPY t1 FROM 'data1.txt'
  30. }
  31. file delete data1.txt
  32. execsql {SELECT DISTINCT log FROM t1 ORDER BY log}
  33. } {0 1 2 3 4 5}
  34. # Basic aggregate functions.
  35. #
  36. do_test select3-1.1 {
  37. execsql {SELECT count(*) FROM t1}
  38. } {31}
  39. do_test select3-1.2 {
  40. execsql {
  41. SELECT min(n),min(log),max(n),max(log),sum(n),sum(log),avg(n),avg(log)
  42. FROM t1
  43. }
  44. } {1 0 31 5 496 124 16 4}
  45. do_test select3-1.3 {
  46. execsql {SELECT max(n)/avg(n), max(log)/avg(log) FROM t1}
  47. } {1.9375 1.25}
  48. # Try some basic GROUP BY clauses
  49. #
  50. do_test select3-2.1 {
  51. execsql {SELECT log, count(*) FROM t1 GROUP BY log ORDER BY log}
  52. } {0 1 1 1 2 2 3 4 4 8 5 15}
  53. do_test select3-2.2 {
  54. execsql {SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log}
  55. } {0 1 1 2 2 3 3 5 4 9 5 17}
  56. do_test select3-2.3 {
  57. execsql {SELECT log, avg(n) FROM t1 GROUP BY log ORDER BY log}
  58. } {0 1 1 2 2 3.5 3 6.5 4 12.5 5 24}
  59. do_test select3-2.3 {
  60. execsql {SELECT log, avg(n)+1 FROM t1 GROUP BY log ORDER BY log}
  61. } {0 2 1 3 2 4.5 3 7.5 4 13.5 5 25}
  62. do_test select3-2.4 {
  63. execsql {SELECT log, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
  64. } {0 0 1 0 2 0.5 3 1.5 4 3.5 5 7}
  65. do_test select3-2.5 {
  66. execsql {SELECT log*2+1, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
  67. } {1 0 3 0 5 0.5 7 1.5 9 3.5 11 7}
  68. do_test select3-2.6 {
  69. execsql {
  70. SELECT log*2+1 as x, count(*) FROM t1 GROUP BY x ORDER BY x
  71. }
  72. } {1 1 3 1 5 2 7 4 9 8 11 15}
  73. do_test select3-2.7 {
  74. execsql {
  75. SELECT log*2+1 AS x, count(*) AS y FROM t1 GROUP BY x ORDER BY y
  76. }
  77. } {1 1 3 1 5 2 7 4 9 8 11 15}
  78. do_test select3-2.8 {
  79. execsql {
  80. SELECT log*2+1 AS x, count(*) AS y FROM t1 GROUP BY x ORDER BY 10-(x+y)
  81. }
  82. } {11 15 9 8 7 4 5 2 3 1 1 1}
  83. do_test select3-2.9 {
  84. catchsql {
  85. SELECT log, count(*) FROM t1 GROUP BY 8 ORDER BY log;
  86. }
  87. } {1 {GROUP BY expressions should not be constant}}
  88. # Cannot have a HAVING without a GROUP BY
  89. #
  90. do_test select3-3.1 {
  91. set v [catch {execsql {SELECT log, count(*) FROM t1 HAVING log>=4}} msg]
  92. lappend v $msg
  93. } {1 {a GROUP BY clause is required before HAVING}}
  94. # Toss in some HAVING clauses
  95. #
  96. do_test select3-4.1 {
  97. execsql {SELECT log, count(*) FROM t1 GROUP BY log HAVING log>=4 ORDER BY log}
  98. } {4 8 5 15}
  99. do_test select3-4.2 {
  100. execsql {
  101. SELECT log, count(*) FROM t1
  102. GROUP BY log
  103. HAVING count(*)>=4
  104. ORDER BY log
  105. }
  106. } {3 4 4 8 5 15}
  107. do_test select3-4.3 {
  108. execsql {
  109. SELECT log, count(*) FROM t1
  110. GROUP BY log
  111. HAVING count(*)>=4
  112. ORDER BY max(n)
  113. }
  114. } {3 4 4 8 5 15}
  115. do_test select3-4.4 {
  116. execsql {
  117. SELECT log AS x, count(*) AS y FROM t1
  118. GROUP BY x
  119. HAVING y>=4
  120. ORDER BY max(n)
  121. }
  122. } {3 4 4 8 5 15}
  123. do_test select3-5.1 {
  124. execsql {
  125. SELECT log, count(*), avg(n), max(n+log*2) FROM t1
  126. GROUP BY log
  127. ORDER BY max(n+log*2), avg(n)
  128. }
  129. } {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}
  130. do_test select3-5.2 {
  131. execsql {
  132. SELECT log, count(*), avg(n), max(n+log*2) FROM t1
  133. GROUP BY log
  134. ORDER BY max(n+log*2), min(log,avg(n))
  135. }
  136. } {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}
  137. finish_test