Re: Is this a hack?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Joe Celko (joe.celko_at_northface.edu)
Date: 02/19/04


Date: Wed, 18 Feb 2004 16:59:21 -0800


>> I need to return a result set with 4 fields [sic]: the name and price
of the highest priced 'a' product and the name and price of the highest
priced 'b' product. <<

Give up putting both answers in one row and you can do it easily.

CREATE TABLE Products
 (p_name CHAR(3) NOT NULL PRIMARY KEY,
 p_class CHAR(2) NOT NULL,
 p_price DECIMAL(8,4) NOT NULL);

INSERT INTO Products VALUES ('P01', 'a', 1.00);
INSERT INTO Products VALUES ('P02', 'a', 2.00);
INSERT INTO Products VALUES ('P03', 'b', 1.00);
INSERT INTO Products VALUES ('P04', 'b', 2.00);
INSERT INTO Products VALUES ('P05', 'b', 3.00);
INSERT INTO Products VALUES ('P06', 'a', 4.00);
INSERT INTO Products VALUES ('P07', 'b', 100.00);
INSERT INTO Products VALUES ('P08', 'a', 2.00);
INSERT INTO Products VALUES ('P09', 'a', 2.00);
INSERT INTO Products VALUES ('P10', 'a', 200.00);

SELECT P1.p_class, P1.p_name, P1.p_price
  FROM Products AS P1
 WHERE P1.p_price
       = (SELECT MAX(P2.p_price)
            FROM Products AS P2
           WHERE P1.p_class = P2.p_class);

Results
p_class p_name p_price
========================
a P10 200.0000
b P07 100.0000

Now, let's figure out what your conceptual mistake was. The result of a
query should be a table. A table does not have repeated attributes in
its rows; just as a table does not have repeated rows; just as a schema
does not have repeated tables. Ergo, your results were not a proper
table -- class and price were repeated!

Hey, you were trying to do formatting for display in the SELECT list.
That always costs you in complexity. This is why it is so important not
to call a column a field; it screws up your mental model.

--CELKO--
 ===========================
 Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Relevant Pages

  • Re: Help me designing that please
    ... CREATE TABLE Orders (order_num INTEGER PRIMARY KEY, ... JOIN DishOrders AS DO ... Drop the price from the Dishes table and keep a separate Price History ... CREATE TABLE PriceHistory (dish_num INTEGER NOT NULL REFERENCES Dishes ...
    (microsoft.public.sqlserver.programming)
  • Design Problem: Products Pricing Affected by Optional Extras
    ... frame options ... Some products do not have any options, some have a different price ... PRIMARY KEY ... UPDATE CASCADE ...
    (comp.databases)
  • Re: Design Problem: Products Pricing Affected by Optional Extras
    ... frame options ... Some products do not have any options, some have a different price ... but with a white frame and blue glass costs ... PRIMARY KEY ...
    (comp.databases)
  • Newbie to Access - updating a field with a calculation
    ... In the project I have a tables for Items, Item Versions and Pricing. ... ID_ITEM_VERSION (autonumber - primary key) ... Price (currency) ... the Cost Per Unit updates. ...
    (microsoft.public.access.formscoding)
  • Re: referenced row
    ... Columnist, SQL Server Professional ... Whenever price in Products is updated, ... CONSTRAINT PK_Products PRIMARY KEY ... sopid INTEGER IDENTITYNOT NULL, ...
    (microsoft.public.sqlserver.programming)