select_having.out 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. --
  2. -- SELECT_HAVING
  3. --
  4. -- load test data
  5. CREATE TABLE test_having (a int, b int, c char(8), d char);
  6. INSERT INTO test_having VALUES (0, 1, 'XXXX', 'A');
  7. INSERT INTO test_having VALUES (1, 2, 'AAAA', 'b');
  8. INSERT INTO test_having VALUES (2, 2, 'AAAA', 'c');
  9. INSERT INTO test_having VALUES (3, 3, 'BBBB', 'D');
  10. INSERT INTO test_having VALUES (4, 3, 'BBBB', 'e');
  11. INSERT INTO test_having VALUES (5, 3, 'bbbb', 'F');
  12. INSERT INTO test_having VALUES (6, 4, 'cccc', 'g');
  13. INSERT INTO test_having VALUES (7, 4, 'cccc', 'h');
  14. INSERT INTO test_having VALUES (8, 4, 'CCCC', 'I');
  15. INSERT INTO test_having VALUES (9, 4, 'CCCC', 'j');
  16. SELECT b, c FROM test_having
  17. GROUP BY b, c HAVING count(*) = 1 ORDER BY b, c;
  18. b | c
  19. ---+----------
  20. 1 | XXXX
  21. 3 | bbbb
  22. (2 rows)
  23. -- HAVING is effectively equivalent to WHERE in this case
  24. SELECT b, c FROM test_having
  25. GROUP BY b, c HAVING b = 3 ORDER BY b, c;
  26. b | c
  27. ---+----------
  28. 3 | BBBB
  29. 3 | bbbb
  30. (2 rows)
  31. SELECT lower(c), count(c) FROM test_having
  32. GROUP BY lower(c) HAVING count(*) > 2 OR min(a) = max(a)
  33. ORDER BY lower(c);
  34. lower | count
  35. -------+-------
  36. bbbb | 3
  37. cccc | 4
  38. xxxx | 1
  39. (3 rows)
  40. SELECT c, max(a) FROM test_having
  41. GROUP BY c HAVING count(*) > 2 OR min(a) = max(a)
  42. ORDER BY c;
  43. c | max
  44. ----------+-----
  45. XXXX | 0
  46. bbbb | 5
  47. (2 rows)
  48. -- test degenerate cases involving HAVING without GROUP BY
  49. -- Per SQL spec, these should generate 0 or 1 row, even without aggregates
  50. SELECT min(a), max(a) FROM test_having HAVING min(a) = max(a);
  51. min | max
  52. -----+-----
  53. (0 rows)
  54. SELECT min(a), max(a) FROM test_having HAVING min(a) < max(a);
  55. min | max
  56. -----+-----
  57. 0 | 9
  58. (1 row)
  59. -- errors: ungrouped column references
  60. SELECT a FROM test_having HAVING min(a) < max(a);
  61. ERROR: column "test_having.a" must appear in the GROUP BY clause or be used in an aggregate function
  62. LINE 1: SELECT a FROM test_having HAVING min(a) < max(a);
  63. ^
  64. SELECT 1 AS one FROM test_having HAVING a > 1;
  65. ERROR: column "test_having.a" must appear in the GROUP BY clause or be used in an aggregate function
  66. LINE 1: SELECT 1 AS one FROM test_having HAVING a > 1;
  67. ^
  68. -- the really degenerate case: need not scan table at all
  69. SELECT 1 AS one FROM test_having HAVING 1 > 2;
  70. one
  71. -----
  72. (0 rows)
  73. SELECT 1 AS one FROM test_having HAVING 1 < 2;
  74. one
  75. -----
  76. 1
  77. (1 row)
  78. -- and just to prove that we aren't scanning the table:
  79. SELECT 1 AS one FROM test_having WHERE 1/a = 1 HAVING 1 < 2;
  80. one
  81. -----
  82. 1
  83. (1 row)
  84. DROP TABLE test_having;