Say I've got a table:
create table foo (
corp_id int not null,
tdate date not null,
sec_id int unsigned not null,
amount int unsigned not null,
primary key (corp_id, sec_id, tdate)
);
The following query will return the sum of the amount column for all corp_id's and dates:
select corp_id, tdate, sum(amount) from foo group by corp_id, tdate;
How can I now limit this query to return only the top 5 latest dates per corp_id?