Eine aufbereitete Darstellung der Quelle

 
     
 
 
Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 

Benutzer

Quelle  select_having_2.out   Sprache: unbekannt

 
Spracherkennung für: .out vermutete Sprache: Unknown {[0] [0] [0]} [Methode: Schwerpunktbildung, einfache Gewichte, sechs Dimensionen]

--
-- SELECT_HAVING
--
-- load test data
CREATE TABLE test_having (a int, b int, c char(8), d char);
INSERT INTO test_having VALUES (01, 'XXXX', 'A');
INSERT INTO test_having VALUES (12, 'AAAA', 'b');
INSERT INTO test_having VALUES (22, 'AAAA', 'c');
INSERT INTO test_having VALUES (33, 'BBBB', 'D');
INSERT INTO test_having VALUES (43, 'BBBB', 'e');
INSERT INTO test_having VALUES (53, 'bbbb', 'F');
INSERT INTO test_having VALUES (64, 'cccc', 'g');
INSERT INTO test_having VALUES (74, 'cccc', 'h');
INSERT INTO test_having VALUES (84, 'CCCC', 'I');
INSERT INTO test_having VALUES (94, 'CCCC', 'j');
SELECT b, c FROM test_having
 GROUP BY b, c HAVING count(*) = 1 ORDER BY b, c;
 b |    c     
---+----------
 1 | XXXX    
 3 | bbbb    
(2 rows)

-- 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     
---+----------
 3 | bbbb    
 3 | BBBB    
(2 rows)

SELECT lower(c), count(c) FROM test_having
 GROUP BY lower(c) HAVING count(*) > 2 OR min(a) = max(a)
 ORDER BY lower(c);
 lower | count 
-------+-------
 bbbb  |     3
 cccc  |     4
 xxxx  |     1
(3 rows)

SELECT c, max(a) FROM test_having
 GROUP BY c HAVING count(*) > 2 OR min(a) = max(a)
 ORDER BY c;
    c     | max 
----------+-----
 bbbb     |   5
 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
LINE 1: SELECT a FROM test_having HAVING min(a) < max(a);
               ^
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
LINE 1: SELECT 1 AS one FROM test_having HAVING a > 1;
                                                ^
-- 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;

[Dauer der Verarbeitung: 0.12 Sekunden, vorverarbeitet 2026-08-09]

                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Open Source Software

     Quellcodebibliothek
     Eigene Quellcodes
     Fremde Quellcodes
     Suchen

Jenseits des Üblichen ....
    

Besucherstatistik

Besucherstatistik

Statistik
#Sources=277311
#Domains=752002