Re: SQL problem: bank account

From: Sean Chittenden <sean(at)chittenden(dot)org>
To: "Erik G(dot) Burrows" <eburrows(at)erikburrows(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: SQL problem: bank account
Date: 2003-06-02 06:21:37
Message-ID: 20030602062137.GD65470@perrin.int.nxad.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> 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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message listrec 2003-06-02 07:49:55 Re: SQL problem: bank account
Previous Message Andrew J. Kopciuch 2003-06-02 06:13:39 Re: SQL problem: bank account