Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id A831113BD197 for ; Fri, 1 Jun 2012 12:13:42 -0300 (ADT) Received: from nm22.bullet.mail.ac4.yahoo.com ([98.139.52.219]) by makus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1SaTXo-00072q-Pw for pgsql-sql@postgresql.org; Fri, 01 Jun 2012 15:13:42 +0000 Received: from [98.139.52.188] by nm22.bullet.mail.ac4.yahoo.com with NNFMP; 01 Jun 2012 15:13:27 -0000 Received: from [98.139.52.160] by tm1.bullet.mail.ac4.yahoo.com with NNFMP; 01 Jun 2012 15:13:27 -0000 Received: from [127.0.0.1] by omp1043.mail.ac4.yahoo.com with NNFMP; 01 Jun 2012 15:13:27 -0000 X-Yahoo-Newman-Id: 809706.15608.bm@omp1043.mail.ac4.yahoo.com Received: (qmail 98497 invoked from network); 1 Jun 2012 15:13:27 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:In-Reply-To:Mime-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Cc:X-Mailer:From:Subject:Date:To; b=T8Oi9emqNAh6nPyGlSlkXbI6mlkpeyzcOy/oh6/2CRIkmYZiYLaGbO0wiq3zhIvIXwl3KAAbcHqB6kwwXajMJo9s9rxyaNmrOqtzKtlOg7FgFeU8yWgUc/8/RVCROFjB8ceFgX48ZlqMz3F/gq2Ut4+JPov91kd7oCHIQhOe9Ac= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1338563607; bh=HYjE3uB1hjDiPQdYU5JwkJijccgd4K3TatLCBc59iDU=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:In-Reply-To:Mime-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Cc:X-Mailer:From:Subject:Date:To; b=HINqtG0efZ0t3cZltCRXcYo+04H8H2KqXYgrA84As4HSMr4Ry+MbJikE0viO/6F35gDCFr2vFFYRbmMrJ1ty5ahBXSBuiXEXQj2c3A1v7K84XzCSnqNFE1c4StpvuMYDhNlnpsEW2HA5vqDCeX90PywADKCFwfbx+GH/B71uFO0= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: LDe2sAYVM1l.Mt41OeYzCLgzouMvl5umLYsYPfjuaLLzjVB ZL_qkkcdVqihpC0LV00M40Rs0sQw8YrzcGlhlVG26O9yP9ZxL5IwZ.IWY_qo taM4lDBrERt7ikVqbZDnn_J1huQ2Ay6ViTp3PoqsJwcj6WdP4a8aGTqrH47k DzY2jMOT7P5kpKYvxM3ZHGHYHx_.qNY8vnCOqR3748rZRS0R_zmbYf_B5L7N FNuS7JyssY.NdwqUfYP3B7rxqpYZjp.tWdBUg4X5Hd1HCg0HZrswNKZ9BICo 2de2NpLV52Z3tByPeJJeLio7_rtbDexlafvtBlaMXyni0wvWdiFJgzUygLcY SW_DK8wwUCP5dXam7rJXUrHFxw.rA2IVMW2QH0FuGA0Cocw4itMAPXc06nNU zXG7OxcrtpDI7sQe0gjp9 X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from [192.12.17.102] (polobo@24.93.23.188 with xymcookie) by smtp103-mob.biz.mail.ac4.yahoo.com with SMTP; 01 Jun 2012 08:13:27 -0700 PDT References: In-Reply-To: Mime-Version: 1.0 (1.0) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Message-Id: <58DBEBA8-661B-4DB0-9E4C-734A65CBA9A3@yahoo.com> Cc: "" X-Mailer: iPad Mail (9B206) From: David Johnston Subject: Re: Lowest 2 items per Date: Fri, 1 Jun 2012 11:13:27 -0400 To: "Relyea, Mike" X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201206/3 X-Sequence-Number: 36657 On Jun 1, 2012, at 10:34, "Relyea, Mike" wrote: > 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. >=20 > 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 ), > ) >=20 > 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 ) > ) >=20 > 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 > ) >=20 > 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. >=20 > SELECT printers.make, printers.model, consumables.color, > consumables.type, min(cost/yield) AS cpp > FROM printers > JOIN printersandconsumables ON printers.printerid =3D > printersandconsumables.printerid > JOIN consumables ON consumables.consumableid =3D > printersandconsumables.consumableid=20 > WHERE consumables.cost Is Not Null=20 > AND consumables.yield Is Not Null > GROUP BY printers.make, printers.model, consumables.color, > consumables.type > ORDER BY make, model; >=20 >=20 > 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! >=20 > Mike >=20 > --=20 > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql I would recommend using the "RANK" window function with an appropriate parti= tion clause in a sub-query then in the outer query you simply WHERE rank <=3D= 2 You will need to decide how to deal with ties. David J.=