From: Sean Chittenden <sean@chittenden.org>
To: Erik G. Burrows <eburrows@erikburrows.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: SQL problem: bank account
Date: Sun, 1 Jun 2003 23:21:37 -0700
Message-ID: <20030602062137.GD65470@perrin.int.nxad.com> (raw)
In-Reply-To: <1054531815.13212.27.camel@griffin.jedi-group.com>
References: <1054531815.13212.27.camel@griffin.jedi-group.com>
> 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: sean@chittenden.org, eburrows@erikburrows.com
Subject: Re: SQL problem: bank account
In-Reply-To: <20030602062137.GD65470@perrin.int.nxad.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox