[thelist] get avg of last 3 records in SQL

shawn allen shawn at alterior.net
Thu Nov 7 14:28:01 CST 2002


quoth Jason Bauer:
[from somebody else]:
> > how can i get the average of the last 3 records entered by a
> > particlar user? or even just the last 3 records and do the math
> > elsewhere.

You might also consider using a view, and simply selecting the avg of
the attribute from there. For instance, in PostgreSQL:

CREATE VIEW most_recent_rows AS
  SELECT relevant_attribute
  FROM relevant_table
  ORDER BY relevant_attribute DESC
  LIMIT 3;

SELECT avg(relevant_attribute) FROM most_recent_rows;

If you're using MySQL, you won't have views at your disposal, but a
temporary table would work similarly on a connection-by-connection
basis. As far as qualifying your query to return only the "last 3
records", well, that varies infinitely with table schema, so I'll leave
that to you ;)

--
shawn allen
  e: shawn at alterior.net
  p: 415 577 3961
  im: shawnpallen



More information about the thelist mailing list