Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Surprising bug

Surprising bug

From: Rich Dillon <richdillon_at_no.spam>
Date: Thu, 06 May 2004 20:54:19 GMT
Message-ID: <%Bxmc.10080$V97.2503@newsread1.news.pas.earthlink.net>


In another forum, someone asked a SQL question which promted me to run the following test. On my version of Oracle (shown below) on my Windows XP laptop, the results pointed to what seems to be an extraordinary bug involving correlated subqueries and views (or derived tables). Oddly, I haven't seen this bug before. Here's the repro script. Does anyone have information on this?

  Connected to:
  Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production   With the Partitioning, OLAP and Oracle Data Mining options   JServer Release 9.2.0.1.0 - Production

  1. Create and populate the following table:

  CREATE TABLE games (
   game_day DATE NOT NULL,
   winner VARCHAR(20) NOT NULL,
   loser VARCHAR(20) NOT NULL,
   winning_score INT NOT NULL,
   losing_score INT NOT NULL,
   PRIMARY KEY (game_day,winner),
   UNIQUE (game_day,loser),
   CHECK (winner <> loser),
   CHECK (winning_score > losing_score));

  INSERT INTO games VALUES ('14-JAN-04','Penguins','Ducks',4,2);
  INSERT INTO games VALUES ('14-JAN-04','Houseflies','Geese',6,1);
  INSERT INTO games VALUES ('21-JAN-04','Penguins','Houseflies',7,0);
  INSERT INTO games VALUES ('21-JAN-04','Ducks','Geese',5,3);
  INSERT INTO games VALUES ('26-JAN-04','Penguins','Geese',6,2);
  INSERT INTO games VALUES ('26-JAN-04','Ducks','Houseflies',5,4);

2) Now, let's say we want a summary of winners. For comparison, we'll create and populate a table.

  CREATE TABLE wintable (
   winner VARCHAR(20) NOT NULL PRIMARY KEY,    wins INT NOT NULL);

  INSERT INTO wintable (winner, wins)
  SELECT winner, COUNT(*)
  FROM games
  GROUP BY winner;

3) And then an equivalent view

  CREATE VIEW winview AS
  SELECT winner, COUNT(*) wins
  FROM games
  GROUP BY winner

4) Verify that the two present the same information.

  SELECT * FROM wintable;
  SELECT * FROM winview;

5) Now, here's the problem. The following two queries should each present the winningest team (or teams if there's a tie). They are identical exept that one uses the table (wintable) and the other uses the view.

  SELECT *
  FROM wintable w1
  WHERE NOT EXISTS (
   SELECT *
   FROM wintable w2
   WHERE w2.wins > w1.wins);

  SELECT *
  FROM winview w1
  WHERE NOT EXISTS (
   SELECT *
   FROM winview w2
   WHERE w2.wins > w1.wins);

On my machine, the first (using the table) is correct. The second (using the view) is incorrect. These are the results:

  WINNER                     WINS

-------------------- ----------
Penguins 3

  1 row selected.

  WINNER                     WINS

-------------------- ----------
Ducks 2 Houseflies 1 Penguins 3

  3 rows selected. Received on Thu May 06 2004 - 15:54:19 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US