Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id A9EE613BD197 for ; Fri, 1 Jun 2012 12:20:48 -0300 (ADT) Received: from iota.marktest.pt ([80.251.174.118] helo=hermes.marktest.pt) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SaTeg-0007Et-Sd for pgsql-sql@postgresql.org; Fri, 01 Jun 2012 15:20:48 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by hermes.marktest.pt (Postfix) with ESMTP id BCC991A83BA; Fri, 1 Jun 2012 16:20:33 +0100 (WEST) X-Virus-Scanned: amavisd-new at marktest.pt Received: from hermes.marktest.pt ([127.0.0.1]) by localhost (hermes.marktest.pt [127.0.0.1]) (amavisd-new, port 11124) with ESMTP id gjWQx3+wRDnE; Fri, 1 Jun 2012 16:20:33 +0100 (WEST) Received: from Proteu (proteu.marktest.pt [10.61.90.54]) by hermes.marktest.pt (Postfix) with ESMTP id 87D611A83B6; Fri, 1 Jun 2012 16:20:33 +0100 (WEST) Message-ID: <112ECE6EE7E74DE28DDDB057DB75870C@marktestcr.marktest.pt> From: "Oliveiros d'Azevedo Cristina" To: "Oliveiros d'Azevedo Cristina" , "Relyea, Mike" , References: <1A73A33A6D424324B55B126B61DC4E2A@marktestcr.marktest.pt> Subject: Re: Lowest 2 items per Date: Fri, 1 Jun 2012 16:20:32 +0100 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=response Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.5512 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579 X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201206/4 X-Sequence-Number: 36658 Sorry, Mike, previous query was flawed. This is (hopefully) the correct version Best, Oliver SELECT make, model, color,type, subquery1.cpp, min(cost/yield) as cpp2 ( SELECT printers.make, printers.model, consumables.color, consumables.type, min(cost/yield) AS cpp FROM printers JOIN printersandconsumables ON printers.printerid = printersandconsumables.printerid JOIN consumables ON consumables.consumableid = printersandconsumables.consumableid WHERE consumables.cost Is Not Null AND consumables.yield Is Not Null GROUP BY printers.make, printers.model, consumables.color, consumables.type ) subquery1 JOIN ( SELECT printers.make, printers.model, consumables.color, consumables.type,cost,yield FROM printers JOIN printersandconsumables ON printers.printerid = printersandconsumables.printerid JOIN consumables ON consumables.consumableid = printersandconsumables.consumableid WHERE consumables.cost Is Not Null AND consumables.yield Is Not Null ) subquery2 ON (subquery1.make = subquery2.make AND subquery1.model = subquery2.model AND subquery1.color = subquery2.color AND subquery1.type = subquery2.type) WHERE subquery2.cost / subquery2.yield <> subquery1.cpp GROUP BY subquery2.make,subquery2. model, subquery2.color,subquery2.type,subquery1.cpp ORDER BY make, model; ----- Original Message ----- From: "Oliveiros d'Azevedo Cristina" To: "Relyea, Mike" ; Sent: Friday, June 01, 2012 3:56 PM Subject: Re: [SQL] Lowest 2 items per > Hi, Mike, > > Can you tell me if this gives what you want, and if it doesn't, what is > the error reported, or wrong result ? > > This is untested query, so Im not sure about it. > > Best, > Oliver > > SELECT make, model, color,type, subquery1.cpp, min(cost/yield) as cpp2 > ( > SELECT printers.make, printers.model, consumables.color, > consumables.type, min(cost/yield) AS cpp > FROM printers > JOIN printersandconsumables ON printers.printerid = > printersandconsumables.printerid > JOIN consumables ON consumables.consumableid = > printersandconsumables.consumableid > WHERE consumables.cost Is Not Null > AND consumables.yield Is Not Null > GROUP BY printers.make, printers.model, consumables.color, > consumables.type > ) subquery1 > NATURAL JOIN > ( > SELECT printers.make, printers.model, consumables.color, > consumables.type > FROM printers > JOIN printersandconsumables ON printers.printerid = > printersandconsumables.printerid > JOIN consumables ON consumables.consumableid = > printersandconsumables.consumableid > WHERE consumables.cost Is Not Null > AND consumables.yield Is Not Null > ) subquery2 > WHERE subquery2.cost / subquery2.yield <> subquery1.cpp > GROUP BY make, model, color,type > ORDER BY make, model; > > > ----- Original Message ----- > From: "Relyea, Mike" > To: > Sent: Friday, June 01, 2012 3:34 PM > Subject: [SQL] Lowest 2 items per > > > I need a little help putting together a query. I have the tables listed > below and I need to return the lowest two consumables (ranked by cost > divided by yield) per printer, per color of consumable, per type of > consumable. > > CREATE TABLE printers > ( > printerid serial NOT NULL, > make text NOT NULL, > model text NOT NULL, > CONSTRAINT printers_pkey PRIMARY KEY (make , model ), > CONSTRAINT printers_printerid_key UNIQUE (printerid ), > ) > > CREATE TABLE consumables > ( > consumableid serial NOT NULL, > brand text NOT NULL, > partnumber text NOT NULL, > color text NOT NULL, > type text NOT NULL, > yield integer, > cost double precision, > CONSTRAINT consumables_pkey PRIMARY KEY (brand , partnumber ), > CONSTRAINT consumables_consumableid_key UNIQUE (consumableid ) > ) > > CREATE TABLE printersandconsumables > ( > printerid integer NOT NULL, > consumableid integer NOT NULL, > CONSTRAINT printersandconsumables_pkey PRIMARY KEY (printerid , > consumableid ), > CONSTRAINT printersandconsumables_consumableid_fkey FOREIGN KEY > (consumableid) > REFERENCES consumables (consumableid) MATCH SIMPLE > ON UPDATE CASCADE ON DELETE CASCADE, > CONSTRAINT printersandconsumables_printerid_fkey FOREIGN KEY > (printerid) > REFERENCES printers (printerid) MATCH SIMPLE > ON UPDATE CASCADE ON DELETE CASCADE > ) > > I've pulled together this query which gives me the lowest consumable per > printer per color per type, but I need the lowest two not just the first > lowest. > > SELECT printers.make, printers.model, consumables.color, > consumables.type, min(cost/yield) AS cpp > FROM printers > JOIN printersandconsumables ON printers.printerid = > printersandconsumables.printerid > JOIN consumables ON consumables.consumableid = > printersandconsumables.consumableid > WHERE consumables.cost Is Not Null > AND consumables.yield Is Not Null > GROUP BY printers.make, printers.model, consumables.color, > consumables.type > ORDER BY make, model; > > > After doing a google search I didn't come up with anything that I was > able to use so I'm asking you fine folks! > > Mike > > -- > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql > > -- > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql