X-Original-To: pgsql-sql@postgresql.org Received: from localhost (developer.postgresql.org [64.117.224.193]) by developer.postgresql.org (Postfix) with ESMTP id 33B399262A6 for ; Mon, 2 Jun 2003 02:21:52 -0400 (EDT) Received: from developer.postgresql.org ([64.117.224.193]) by localhost (developer.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 98500-03 for ; Mon, 2 Jun 2003 02:21:39 -0400 (EDT) Received: from perrin.int.nxad.com (internal.ext.nxad.com [69.1.70.251]) by developer.postgresql.org (Postfix) with ESMTP id 0016E9262C1 for ; Mon, 2 Jun 2003 02:21:38 -0400 (EDT) Received: by perrin.int.nxad.com (Postfix, from userid 1001) id 5B5EB21058; Sun, 1 Jun 2003 23:21:37 -0700 (PDT) Date: Sun, 1 Jun 2003 23:21:37 -0700 From: Sean Chittenden To: "Erik G. Burrows" Cc: pgsql-sql@postgresql.org Subject: Re: SQL problem: bank account Message-ID: <20030602062137.GD65470@perrin.int.nxad.com> References: <1054531815.13212.27.camel@griffin.jedi-group.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1054531815.13212.27.camel@griffin.jedi-group.com> X-PGP-Key: finger seanc@FreeBSD.org X-PGP-Fingerprint: 3849 3760 1AFE 7B17 11A0 83A6 DD99 E31F BC84 B341 X-Web-Homepage: http://sean.chittenden.org/ User-Agent: Mutt/1.5.4i X-Virus-Scanned: by amavisd-new X-Archive-Number: 200306/13 X-Sequence-Number: 13771 > It seems to me this is a simple problem, but the solution eludes me. > > I have a table: > > bank_account ( > transaction_id int not null serial, > customer_id int not null references customer(id), > ts timestamp not null default now(), > amount float not null, > balance float not null, > primary key(transaction_id) > ) > > I need to get the most recent transaction for each customer. I need only > the transaction ID, but the entire row would be best. For the sake of being explicit, change your table definition (though what you have above is a-okay and works): CREATE SEQUENCE transaction_id_seq; CREATE TABLE bank_account ( transaction_id int not null DEFAULT NEXTVAL('transaction_id_seq'::TEXT), customer_id int not null references customer(id), ts timestamp not null default now(), amount float not null, balance float not null, primary key(transaction_id) ); Once you insert a value into the bank_account table, SELECT CURRVAL('transaction_id_seq') will be what you're looking for. Read up on CURRVAL() at: http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=functions-sequence.html -sc -- Sean Chittenden