Help! Calculate Average using Queries

A

Alex_Firenze

Hi all,

In my DB I have a table that contains for each date the price returns
of a set of stocks:

example:

DATE, STOCK LABEL, RETURNS
date1, AAA, 10
date1, BBB, 5
date1, CCC, 3
date2, AAA, 9
date2, BBB, 6
date2, CCC, 4
date3, AAA, 6
date3, BBB, 4
date3, CCC, 2
date4, AAA, 11
date4, BBB, 6
date4, CCC, 4
.....

I would like to make a query that give me for each stock the average of
the last 15 days.

In this case the output should be:

STOCK, AVERAGE(Returns,15 days)

Could you help me?? I have tried to do something but without a good
result....

thanks in advance

Alex
 
C

Chaim

Alex,

Try

SELECT [Stock Label], Avg(Returns)
FROM [Your Table]
WHERE [ReturnDate] BETWEEN Date() AND Date()-15
GROUP BY [Stock Label];

Do not use 'DATE' as the name of a field. It is not descriptive (date of
what?) and it conflicts with a reserved word. Also, beware of nulls (for
example, an item of some type was returned on a date but don't yet know how
many so total return value is not known yet).

Good Luck!
 
A

Alex_Firenze

It seems it work! thank you very much Chaim!
Indeed, I don't use a field DATE, it was just to make an example...

Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top