]> granicus.if.org Git - postgresql/blobdiff - src/test/regress/expected/select_having_2.out
Make the behavior of HAVING without GROUP BY conform to the SQL spec.
[postgresql] / src / test / regress / expected / select_having_2.out
index 239e49f3d368cf2da07daf06fc5367b21c95c7f8..542436e5ea4f762754f7f8115019475592348fa5 100644 (file)
@@ -21,7 +21,7 @@ SELECT b, c FROM test_having
  3 | bbbb    
 (2 rows)
 
--- HAVING is equivalent to WHERE in this case
+-- HAVING is effectively equivalent to WHERE in this case
 SELECT b, c FROM test_having
        GROUP BY b, c HAVING b = 3 ORDER BY b, c;
  b |    c     
@@ -49,4 +49,41 @@ SELECT c, max(a) FROM test_having
  XXXX     |   0
 (2 rows)
 
+-- 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
+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
+-- 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)
+
 DROP TABLE test_having;