
306 PUZZLE 74 DERIVED TABLES OR NOT?
PUZZLE
74 DERIVED TABLES OR NOT?
Allen Davidson was trying to join three tables with two LEFT OUTER
JOINs and an INNER JOIN to get the SUM() of a few of the columns. Can
his query be rewritten to avoid the derived tables?
CREATE TABLE Accounts
(acct_nbr INTEGER NOT NULL PRIMARY KEY);
INSERT INTO Accounts VALUES(1), (2), (3), (4);
Please notice that the following, Foo and Bar, are not tables, since
they have no keys.
CREATE TABLE Foo
(acct_nbr INTEGER NOT NULL
REFERENCES Accounts(acct_nbr),
foo_qty INTEGER NOT NULL);
INSERT INTO Foo VALUES (1, 10);
INSERT INTO Foo VALUES (2, 20);
INSERT INTO Foo VALUES (2, 40);
INSERT ...