Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id F3A78672B1F for ; Fri, 1 Jun 2012 15:31:47 -0300 (ADT) Received: from mail-ee0-f46.google.com ([74.125.83.46]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SaWdU-0001Ga-I8 for pgsql-sql@postgresql.org; Fri, 01 Jun 2012 18:31:46 +0000 Received: by eeit10 with SMTP id t10so996397eei.19 for ; Fri, 01 Jun 2012 11:31:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=tU37E5p4B0aIBRUPMtoUMd+w9WzXP0CxE4op9Wszo4Q=; b=MzYhXq+EC26FEEgYFqPSmKhT4ASoLgPT2vxCCYIVNkULA4PlrH01zruosJNwB28lLo G4Hgh/T9PRtOKlG9M3FpdZaWM4/7f7vB+cwvx7/uNpOuFH+hnkOjhLtvyJABg5WgJoXs 2Mpufznk8zh3oMiAD1o3L0egy6uX5Rgd6fzNX1VcCn0zYN588kUgsU25DS0SMSemHMMs vDaK/RD8HMe24UiJxjP3Oy2wNJKFpEXosEX3gO+NzjqqmP2jCDwNrk1wvXz9yuUZS2eQ G/4ydI6W/AMxmLQnEbIBMmw/KQ5VYR4LstAQf4NCjiJU0Ibstmkb3Fwyq2Ofuj09NYiW P+Bg== Received: by 10.14.98.200 with SMTP id v48mr2013516eef.6.1338575491756; Fri, 01 Jun 2012 11:31:31 -0700 (PDT) Received: from [192.168.178.16] (qtuning.xs4all.nl. [80.101.37.209]) by mx.google.com with ESMTPS id q53sm9626372eef.8.2012.06.01.11.31.30 (version=SSLv3 cipher=OTHER); Fri, 01 Jun 2012 11:31:31 -0700 (PDT) Message-ID: <4FC90A7F.9020605@gmail.com> Date: Fri, 01 Jun 2012 20:31:27 +0200 From: Mario Dankoor User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20120428 Thunderbird/12.0.1 MIME-Version: 1.0 To: "Relyea, Mike" CC: pgsql-sql@postgresql.org Subject: Re: Lowest 2 items per References: <58DBEBA8-661B-4DB0-9E4C-734A65CBA9A3@yahoo.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) X-Archive-Number: 201206/10 X-Sequence-Number: 36664 On 2012-06-01 5:44 PM, Relyea, Mike wrote: >> -----Original Message----- >> From: David Johnston [mailto:polobo@yahoo.com] >> Sent: Friday, June 01, 2012 11:13 AM >> To: Relyea, Mike >> Cc: >> Subject: Re: [SQL] Lowest 2 items per >> >> >> I would recommend using the "RANK" window function with an appropriate >> partition clause in a sub-query then in the outer query you simply > WHERE >> rank<= 2 >> >> You will need to decide how to deal with ties. >> >> David J. > > > David, > > I've never used window functions before and rank looks like it'd do the > job quite nicely. Unfortunately I'm using 8.3 - which I should have > mentioned in my original request but didn't. Window functions weren't > introduced until 8.4 from what I can tell. > > Mike > Mike, try following query it's a variation on a top N ( = 3) query SELECT FRS.* FROM ( SELECT PRN.make ,PRN.model ,CSM.color ,CSM.type ,cost/yield rank FROM consumable CSM ,printers PRN ,printersandconsumable PCM WHERE 1 = 1 AND PCM.printerid = PRN.printerid AND PCM.consumableid = CSM.consumableid group by PRN.make ,PRN.model ,CSM.color ,CSM.type ) FRS WHERE 3 > ( SELECT COUNT(*) FROM ( SELECT PRN.make ,PRN.model ,CSM.color ,CSM.type ,cost/yield rank FROM consumable CSM ,printers PRN ,printersandconsumable PCM WHERE 1 = 1 AND PCM.printerid = PRN.printerid AND PCM.consumableid = CSM.consumableid group by PRN.make ,PRN.model ,CSM.color ,CSM.type ) NXT WHERE 1 = 1 AND NXT.make = FRS.make AND NXT.model= FRS.model AND NXT.color= FRS.color AND NXT.type = FRS.type AND NXT.cost <= FRS.cost )