Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 91B7912A65BB for ; Sat, 2 Jun 2012 10:57:17 -0300 (ADT) Received: from forward3h.mail.yandex.net ([2a02:6b8:0:f05::3]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SaopP-0002oN-8p for pgsql-sql@postgresql.org; Sat, 02 Jun 2012 13:57:16 +0000 Received: from web11h.yandex.ru (web11h.yandex.ru [84.201.186.40]) by forward3h.mail.yandex.net (Yandex) with ESMTP id 77C58136137F; Sat, 2 Jun 2012 17:56:59 +0400 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1338645419; bh=CgGCUB4LO/nl1S/Pqre+HKH2x0CBhmAvL5PTTzabpQI=; h=From:To:Cc:In-Reply-To:References:Subject:MIME-Version:Message-Id: Date:Content-Transfer-Encoding:Content-Type; b=ANfhszbIwYR+12ufggwcIn2/3W1wmCpSovheiVxeBm3DTkr308kqJGVkrK3DpThvi y/1AiGevlNI+E1b/5JetPzgIvxGYN4DSCJ9wtC16HnpCLTiuT9FxMFVQ+l8Xt3cMQE 7V9ShB3dQuo/0KUTsTxvgzWeBDMJayqg+zjPpH58= Received: from 127.0.0.1 (localhost.localdomain [127.0.0.1]) by web11h.yandex.ru (Yandex) with ESMTP id 1B61A3030001; Sat, 2 Jun 2012 17:56:59 +0400 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1338645419; bh=CgGCUB4LO/nl1S/Pqre+HKH2x0CBhmAvL5PTTzabpQI=; h=From:To:Cc:In-Reply-To:References:Subject:MIME-Version:Message-Id: Date:Content-Transfer-Encoding:Content-Type; b=ANfhszbIwYR+12ufggwcIn2/3W1wmCpSovheiVxeBm3DTkr308kqJGVkrK3DpThvi y/1AiGevlNI+E1b/5JetPzgIvxGYN4DSCJ9wtC16HnpCLTiuT9FxMFVQ+l8Xt3cMQE 7V9ShB3dQuo/0KUTsTxvgzWeBDMJayqg+zjPpH58= Received: from 128-73-88-66.broadband.corbina.ru (128-73-88-66.broadband.corbina.ru [128.73.88.66]) by web11h.yandex.ru with HTTP; Sat, 02 Jun 2012 17:56:58 +0400 From: msi77 To: "Relyea, Mike" Cc: "pgsql-sql@postgresql.org" In-Reply-To: References: Subject: Re: Lowest 2 items per MIME-Version: 1.0 Message-Id: <299741338645418@web11h.yandex.ru> Date: Sat, 02 Jun 2012 17:56:58 +0400 X-Mailer: Yamail [ http://yandex.ru ] 5.0 Content-Type: text/plain; charset=koi8-r Content-Transfer-Encoding: quoted-printable X-Pg-Spam-Score: -1.5 (-) X-Archive-Number: 201206/13 X-Sequence-Number: 36667 A few of approaches to solve this problem: http://sql-ex.com/help/select16.php 01.06.2012, 18:34, "Relyea, Mike" : > I need a little help putting together a query. =9AI have the tables lis= ted > 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 > ( > =9A=9Aprinterid serial NOT NULL, > =9A=9Amake text NOT NULL, > =9A=9Amodel text NOT NULL, > =9A=9ACONSTRAINT printers_pkey PRIMARY KEY (make , model ), > =9A=9ACONSTRAINT printers_printerid_key UNIQUE (printerid ), > ) > > CREATE TABLE consumables > ( > =9A=9Aconsumableid serial NOT NULL, > =9A=9Abrand text NOT NULL, > =9A=9Apartnumber text NOT NULL, > =9A=9Acolor text NOT NULL, > =9A=9Atype text NOT NULL, > =9A=9Ayield integer, > =9A=9Acost double precision, > =9A=9ACONSTRAINT consumables_pkey PRIMARY KEY (brand , partnumber ), > =9A=9ACONSTRAINT consumables_consumableid_key UNIQUE (consumableid ) > ) > > CREATE TABLE printersandconsumables > ( > =9A=9Aprinterid integer NOT NULL, > =9A=9Aconsumableid integer NOT NULL, > =9A=9ACONSTRAINT printersandconsumables_pkey PRIMARY KEY (printerid , > consumableid ), > =9A=9ACONSTRAINT printersandconsumables_consumableid_fkey FOREIGN KEY > (consumableid) > =9A=9A=9A=9A=9A=9AREFERENCES consumables (consumableid) MATCH SIMPLE > =9A=9A=9A=9A=9A=9AON UPDATE CASCADE ON DELETE CASCADE, > =9A=9ACONSTRAINT printersandconsumables_printerid_fkey FOREIGN KEY > (printerid) > =9A=9A=9A=9A=9A=9AREFERENCES printers (printerid) MATCH SIMPLE > =9A=9A=9A=9A=9A=9AON UPDATE CASCADE ON DELETE CASCADE > ) > > I've pulled together this query which gives me the lowest consumable pe= r > printer per color per type, but I need the lowest two not just the firs= t > lowest. > > 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 > 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