Query to retrieve average of last 3 records (1 Viewer)

cpampas

Registered User.
Local time
Today, 02:05
Joined
Jul 23, 2012
Messages
218
Hello,
I ve been struggling with this for a while now. i wonder if someone can help.
I have a table with the following data :
idjogoPlapwANum
252784Almere City Fc - Telstar0,4375384610
250939Almere City Fc - Telstar0,4270380791
248979Almere City Fc - Telstar0,4350466252
246034Almere City Fc - Telstar0,4626709733
244191Almere City Fc - Telstar0,4626709734
242250Almere City Fc - Telstar0,4474497775
239485Almere City Fc - Telstar0,4474497776
334186Aberdeen - Kilmarnock0,5280462660
331538Aberdeen - Kilmarnock0,5339652151
328899Aberdeen - Kilmarnock0,5160462952
326269Aberdeen - Kilmarnock0,5160462953
323661Aberdeen - Kilmarnock0,5160462954
321786Aberdeen - Kilmarnock0,5160462955
318991Aberdeen - Kilmarnock0,5244725916
316110Aberdeen - Kilmarnock0,5244725917
313217Aberdeen - Kilmarnock0,5244725918

I would like to get the average of the fields (pw), with the 3 highest (ANum), in this case :

Almere City Fc - Telstar
0,452523509​
Aberdeen - Kilmarnock
0,524472591​

I've tried the following query using dcount to count the number of records in each field (jogoPla), and the result is OK, except for the fact that it takes 1 minute to execute this query alone

SELECT tblLastW_Pinn.jogoPla, Avg(tblLastW_Pinn.pw) AS MédiaDepw
FROM tblLastW_Pinn
WHERE (((tblLastW_Pinn.ANum)>DCount("[jogopla]","tblLastW_Pinn","[jogopla]=""" & [jogoPla] & """")-4))
GROUP BY tblLastW_Pinn.jogoPla;

Perhaps there is another way to make it faster
Thanks
 

CJ_London

Super Moderator
Staff member
Local time
Today, 10:05
Joined
Feb 19, 2013
Messages
16,607
main thing that affects speed is indexing, your jogopla and anum fields should both be indexed

to get the last 3 records you need to use a subquery within your query, so try something like this

[CODE
SELECT jogoPla, Avg(pw) AS AvgOfpw
FROM myTable
WHERE ID In (SELECT TOP 3 ID FROM mytable T WHERE jogopla=myTable.jogopla ORDER BY Anum Desc)
GROUP BY jogoPla;
[/CODE]

edit: corrected code for missing group by clause and field name typo
 
Last edited:

plog

Banishment Pending
Local time
Today, 04:05
Joined
May 11, 2011
Messages
11,638
You have assumed that ANum will always be consecutive for an jogoPla--is that accurate? Records won't be deleted? Records will always be added with the next Anum? These things you are certain of, or just assumed? The first thing I'd do is test my data to make sure those assumptions are correct:

Code:
SELECT jogoPla
FROM tblLastW_Pinn 
GROUP BY jogoPla
HAVING (COUNT(jogoPla) - MAX(Anum) <> 1)

If that query returns any records your assumptions are incorrect. If it returns none, you can proceed with your method. Then I would change your method to not use a DCOUNT, but a subquery:

Code:
SELECT jogoPla, MAX(Anum) - 4 AS AnumBase
FROM tblLastW_Pinn 
GROUP BY jogoPla

Save that query, then change the query you posted above to use it. Add it in an INNER JOIN and then join it by jogoPla fields (equally) and ANum to AnumBase (ANum > AnumBAse). That should make your query more efficient.
 

cpampas

Registered User.
Local time
Today, 02:05
Joined
Jul 23, 2012
Messages
218
Hi again,
I ended up following plog's advice , and since Anum is consecutive, and no deletions are allowded, I created the two queries :

Code:
SELECT tblLastW_Pinn.jogoPla, Max([Anum])-4 AS AnumBase
FROM tblLastW_Pinn
GROUP BY tblLastW_Pinn.jogoPla;

Code:
SELECT tblLastW_Pinn.jogoPla, Avg(tblLastW_Pinn.pw) AS MédiaDepw, qryWFirst_pinn1.AnumBase
FROM qryWFirst_pinn1 INNER JOIN tblLastW_Pinn ON qryWFirst_pinn1.jogoPla = tblLastW_Pinn.jogoPla
WHERE (((tblLastW_Pinn.ANum)>[AnumBAse]))
GROUP BY tblLastW_Pinn.jogoPla, qryWFirst_pinn1.AnumBase;

I also indexed jogopla and anum fields as cj_London said
And It all works like a charm

Tanks for your kind help
 

Users who are viewing this thread

Top Bottom