Shane Wegner wrote:
>
> Hi,
>
> I am trying to write a routine to calculate an average based on the 5
> entries before a certain entry in a table based on the _date column.
>
> Is this possible with a select statement? The limit keyword has an offset
> and a limit but can you tell select to return the 5 entries before a
> certain entry.
>
> Example:
> create table chart(
> _date date,
> symbol varchar(10),
> close double(22,3),
> 5avg double(22,3);
>
> 5avg should be the last 5 closing values for a symbol. Any ideas on how
> to do this most efficiently with MySQL would be greatly appreciated.
> Please cc me on replies.
>
> Thanks in advance,
> Shane
>
> --
> Shane Wegner: shane@stripped
Hi Shane
With certainEntry_date set to the _date value from the one you search the last 5 closing
values, you can use this:
SELECT * FROM chart WHERE _date < certainEntry_date ORDER BY _date DESC LIMIT 5;
Tschau
Christian