2000-01-06 07:40:54 +01:00
|
|
|
--
|
|
|
|
-- SELECT_HAVING
|
|
|
|
--
|
|
|
|
-- load test data
|
|
|
|
CREATE TABLE test_having (a int, b int, c char(8), d char);
|
|
|
|
INSERT INTO test_having VALUES (0, 1, 'XXXX', 'A');
|
|
|
|
INSERT INTO test_having VALUES (1, 2, 'AAAA', 'b');
|
|
|
|
INSERT INTO test_having VALUES (2, 2, 'AAAA', 'c');
|
|
|
|
INSERT INTO test_having VALUES (3, 3, 'BBBB', 'D');
|
|
|
|
INSERT INTO test_having VALUES (4, 3, 'BBBB', 'e');
|
|
|
|
INSERT INTO test_having VALUES (5, 3, 'bbbb', 'F');
|
|
|
|
INSERT INTO test_having VALUES (6, 4, 'cccc', 'g');
|
|
|
|
INSERT INTO test_having VALUES (7, 4, 'cccc', 'h');
|
|
|
|
INSERT INTO test_having VALUES (8, 4, 'CCCC', 'I');
|
|
|
|
INSERT INTO test_having VALUES (9, 4, 'CCCC', 'j');
|
|
|
|
SELECT b, c FROM test_having
|
2002-11-21 01:42:20 +01:00
|
|
|
GROUP BY b, c HAVING count(*) = 1 ORDER BY b, c;
|
2000-01-06 07:40:54 +01:00
|
|
|
b | c
|
|
|
|
---+----------
|
|
|
|
1 | XXXX
|
|
|
|
3 | bbbb
|
1998-08-29 06:10:40 +02:00
|
|
|
(2 rows)
|
1998-07-08 16:29:09 +02:00
|
|
|
|
2005-03-11 00:21:26 +01:00
|
|
|
-- HAVING is effectively equivalent to WHERE in this case
|
2002-06-26 23:58:56 +02:00
|
|
|
SELECT b, c FROM test_having
|
2002-11-21 01:42:20 +01:00
|
|
|
GROUP BY b, c HAVING b = 3 ORDER BY b, c;
|
2002-06-26 23:58:56 +02:00
|
|
|
b | c
|
|
|
|
---+----------
|
|
|
|
3 | BBBB
|
|
|
|
3 | bbbb
|
|
|
|
(2 rows)
|
|
|
|
|
2000-01-06 07:40:54 +01:00
|
|
|
SELECT lower(c), count(c) FROM test_having
|
2002-11-21 01:42:20 +01:00
|
|
|
GROUP BY lower(c) HAVING count(*) > 2 OR min(a) = max(a)
|
|
|
|
ORDER BY lower(c);
|
2003-05-26 02:11:29 +02:00
|
|
|
lower | count
|
|
|
|
-------+-------
|
|
|
|
bbbb | 3
|
|
|
|
cccc | 4
|
|
|
|
xxxx | 1
|
1998-08-29 06:10:40 +02:00
|
|
|
(3 rows)
|
|
|
|
|
2000-01-06 07:40:54 +01:00
|
|
|
SELECT c, max(a) FROM test_having
|
2002-11-21 01:42:20 +01:00
|
|
|
GROUP BY c HAVING count(*) > 2 OR min(a) = max(a)
|
|
|
|
ORDER BY c;
|
2000-01-06 07:40:54 +01:00
|
|
|
c | max
|
|
|
|
----------+-----
|
|
|
|
XXXX | 0
|
|
|
|
bbbb | 5
|
1998-08-29 06:10:40 +02:00
|
|
|
(2 rows)
|
|
|
|
|
2005-03-11 00:21:26 +01:00
|
|
|
-- test degenerate cases involving HAVING without GROUP BY
|
|
|
|
-- Per SQL spec, these should generate 0 or 1 row, even without aggregates
|
|
|
|
SELECT min(a), max(a) FROM test_having HAVING min(a) = max(a);
|
|
|
|
min | max
|
|
|
|
-----+-----
|
|
|
|
(0 rows)
|
|
|
|
|
|
|
|
SELECT min(a), max(a) FROM test_having HAVING min(a) < max(a);
|
|
|
|
min | max
|
|
|
|
-----+-----
|
|
|
|
0 | 9
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
-- errors: ungrouped column references
|
|
|
|
SELECT a FROM test_having HAVING min(a) < max(a);
|
|
|
|
ERROR: column "test_having.a" must appear in the GROUP BY clause or be used in an aggregate function
|
2008-09-01 22:42:46 +02:00
|
|
|
LINE 1: SELECT a FROM test_having HAVING min(a) < max(a);
|
|
|
|
^
|
2005-03-11 00:21:26 +01:00
|
|
|
SELECT 1 AS one FROM test_having HAVING a > 1;
|
|
|
|
ERROR: column "test_having.a" must appear in the GROUP BY clause or be used in an aggregate function
|
2008-09-01 22:42:46 +02:00
|
|
|
LINE 1: SELECT 1 AS one FROM test_having HAVING a > 1;
|
|
|
|
^
|
2005-03-11 00:21:26 +01:00
|
|
|
-- the really degenerate case: need not scan table at all
|
|
|
|
SELECT 1 AS one FROM test_having HAVING 1 > 2;
|
|
|
|
one
|
|
|
|
-----
|
|
|
|
(0 rows)
|
|
|
|
|
|
|
|
SELECT 1 AS one FROM test_having HAVING 1 < 2;
|
|
|
|
one
|
|
|
|
-----
|
|
|
|
1
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
-- and just to prove that we aren't scanning the table:
|
|
|
|
SELECT 1 AS one FROM test_having WHERE 1/a = 1 HAVING 1 < 2;
|
|
|
|
one
|
|
|
|
-----
|
|
|
|
1
|
|
|
|
(1 row)
|
|
|
|
|
2000-01-06 07:40:54 +01:00
|
|
|
DROP TABLE test_having;
|