This is the solution I was looking for! 20 seconds to compile the whole
list.
Thanks everyone for your help, I very much appreciate it. Even though
this is kind of 'hackey' being that it's non-standard SQL, it keeps the
database from having to to 40,000 selects, as would have to do with any
corre
On Monday 02 June 2003 00:49, listrec wrote:
> How about:
>
> select max(transaction_id) from bank_account group by customer_id
>
And if you want the rest of the data in the rows:
SELECT b.*
FROM bank_account b
JOIN (
SELECT max(transaction_id) AS transaction_id
FROM bank_account
On Monday 02 June 2003 00:49, listrec wrote:
> How about:
>
> select max(transaction_id) from bank_account group by customer_id
>
And if you want the rest of the data in the rows:
SELECT b.*
FROM bank_account b
JOIN (
SELECT max(transaction_id) AS transaction_id
FROM bank_account
I am afraid, this looks even uglier then your second solution, but
should work, and be quicker...
-- You need this to avoid having to rescan the whole table for each
customerid every time and resort the results
create index customer_txstamp_idx on bank_account (customer_id, ts);
select ba.* fro
"Erik G. Burrows" <[EMAIL PROTECTED]> writes:
> I need to get the most recent transaction for each customer. I need only
> the transaction ID, but the entire row would be best.
If you don't mind a not-standard-SQL solution, the SELECT DISTINCT ON
construct is designed for this sort of thing. See
problem: bank account
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
> 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
> I need to get the most recent transaction for each customer. I need only
> the transaction ID, but the entire row would be best.
>
Why don't you alter the customer table to hold the transaction ID of the most
recent transaction?
Some questions though:
Do you vacuum the database regularly?
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(tra