Скажем, у меня есть стол:
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)
);
Следующий запрос вернет сумму столбца суммы для всех corp_id и дат:
select corp_id, tdate, sum(amount) from foo group by corp_id, tdate;
Как теперь можно ограничить этот запрос, чтобы он возвращал только первые 5 самых последних дат для corp_id?