Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 5665312A65BB for ; Sat, 2 Jun 2012 13:32:49 -0300 (ADT) Received: from forward1.mail.yandex.net ([2a02:6b8:0:602::1]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SarFu-0005Fk-IR for pgsql-sql@postgresql.org; Sat, 02 Jun 2012 16:32:47 +0000 Received: from web12d.yandex.ru (web12d.yandex.ru [77.88.47.157]) by forward1.mail.yandex.net (Yandex) with ESMTP id D215212406A2; Sat, 2 Jun 2012 20:32:31 +0400 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1338654751; bh=GZZpEOFmtmQAjrzbM/zaHcQKwQQ4AQAuoTt8+zpngMM=; h=From:To:Cc:In-Reply-To:References:Subject:MIME-Version:Message-Id: Date:Content-Transfer-Encoding:Content-Type; b=DbrUQXsSnkf0ZF+jLIWnvr0WzWC2aMlt3tbIX3+oLb9apYvTRvBhSy7eYzyylFF8V fA+aiS3PRhOBOlLlJn5pX9DcN+48k/MLm0A0Jl778OK+EPw7Xpza6Yi1b2PszX4F5H PKg+rjywqEQ5iSlG258FhKmMwqHR5dYOI+NfYb6k= Received: from 127.0.0.1 (localhost.localdomain [127.0.0.1]) by web12d.yandex.ru (Yandex) with ESMTP id 952FF6ED0F04; Sat, 2 Jun 2012 20:32:31 +0400 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1338654751; bh=GZZpEOFmtmQAjrzbM/zaHcQKwQQ4AQAuoTt8+zpngMM=; h=From:To:Cc:In-Reply-To:References:Subject:MIME-Version:Message-Id: Date:Content-Transfer-Encoding:Content-Type; b=DbrUQXsSnkf0ZF+jLIWnvr0WzWC2aMlt3tbIX3+oLb9apYvTRvBhSy7eYzyylFF8V fA+aiS3PRhOBOlLlJn5pX9DcN+48k/MLm0A0Jl778OK+EPw7Xpza6Yi1b2PszX4F5H PKg+rjywqEQ5iSlG258FhKmMwqHR5dYOI+NfYb6k= Received: from [2.94.136.27] ([2.94.136.27]) by web12d.yandex.ru with HTTP; Sat, 02 Jun 2012 20:32:31 +0400 From: msi77 To: Oliveiros Cc: "pgsql-sql@postgresql.org" In-Reply-To: References: <299741338645418@web11h.yandex.ru> Subject: Re: Lowest 2 items per MIME-Version: 1.0 Message-Id: <308891338654751@web12d.yandex.ru> Date: Sat, 02 Jun 2012 20:32:31 +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/15 X-Sequence-Number: 36669 Thank you for reply, Oliver. I want that you'll pay attention to the learn exercises which can by made= under PostgreSQL among few other DBMS: http://sql-ex.ru/exercises/index.php?act=3Dlearn=20 02.06.2012, 19:00, "Oliveiros" : > Nice resource, msi77. > > Thanx for sharing. > > I wasn't aware of none of these techniques, actually, so I tried to sta= rt from scratch, but I should've realized that many people in the past ha= d the same problem as Mike and I should have googled a little instead of = trying to re-invent the wheel. > > Anyway, this is great information and I'm sure it will be useful in the= future. > Again thanx for sharing. > > Best, > Oliver > > 2012/6/2 msi77 >> 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 l= isted >>> 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 = per >>> printer per color per type, but I need the lowest two not just the fi= rst >>> 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 >> >> -- >> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) >> To make changes to your subscription: >> http://www.postgresql.org/mailpref/pgsql-sql