Re: [SQL] SQL problem: bank account

2003-06-03 Thread Tom Lane
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 the

Re: [SQL] SQL problem: bank account

2003-06-03 Thread Dmitry Tkach
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.*

Re: [SQL] SQL problem: bank account

2003-06-03 Thread Jonathan Gardner
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

Re: [SQL] SQL problem: bank account

2003-06-03 Thread Jonathan Gardner
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

Re: [SQL] SQL problem: bank account

2003-06-03 Thread Erik G. Burrows
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

[SQL] SQL problem: bank account

2003-06-02 Thread Erik G. Burrows
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

Re: [SQL] SQL problem: bank account

2003-06-02 Thread Sean Chittenden
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,

Re: [SQL] SQL problem: bank account

2003-06-02 Thread listrec
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