X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 3B58DD7B14 for ; Wed, 24 Aug 2005 20:52:29 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 35206-10 for ; Wed, 24 Aug 2005 23:52:27 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id 221E3D7B0D for ; Wed, 24 Aug 2005 20:52:24 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j7ONqN3r016784 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 24 Aug 2005 17:52:26 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.3/8.13.3) with ESMTP id j7ONqN37019131; Wed, 24 Aug 2005 17:52:23 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.3/8.13.3/Submit) id j7ONqNrs019130; Wed, 24 Aug 2005 17:52:23 -0600 (MDT) (envelope-from mfuhr) Date: Wed, 24 Aug 2005 17:52:23 -0600 From: Michael Fuhr To: Lane Van Ingen Cc: pgsql-sql@postgresql.org Subject: Re: Can EXCEPT Be Used for To Solve This Problem? Message-ID: <20050824235223.GA19076@winnie.fuhr.org> References: <20050824233449.GA12396@winnie.fuhr.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050824233449.GA12396@winnie.fuhr.org> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.005 required=5 tests=[AWL=0.005] X-Spam-Level: X-Archive-Number: 200508/235 X-Sequence-Number: 22463 On Wed, Aug 24, 2005 at 05:34:49PM -0600, Michael Fuhr wrote: > On Wed, Aug 24, 2005 at 04:47:16PM -0400, Lane Van Ingen wrote: > > Given the following data in a table named 'foo' : > > id update_time description > > 2 2005-08-24 00:10:00 transaction1 > > 2 2005-08-24 00:22:00 transaction2 > > 2 2005-08-24 00:34:00 transaction3 > > 2 2005-08-24 00:58:00 transaction4 > > > > I want to select 2nd oldest transaction from foo (transaction 3). The I just noticed that transaction3 isn't the 2nd oldest, it's the 3rd oldest and the 2nd newest. What are you really trying to do? > > solution below > > works, but I think there may be a better way. Does anyone else have a better > > idea? > > Do you want the 2nd oldest transaction from the entire table? If > so then the following should work: > > SELECT * > FROM foo > ORDER BY update_time > OFFSET 1 > LIMIT 1; Flaw: this query assumes that the 2nd record in the ordered result set is the 2nd oldest transaction, which isn't necessarily true. If the update_time values aren't unique, then the 2nd record could have the oldest time and not the 2nd oldest time. Is that why you were using EXCEPT? To exclude all instances of the oldest time? -- Michael Fuhr