Data collecting for Reporting

I have a reporting question. I want to allow the user to create a Top 10 Report based on a time period, i.e. Top 10 Report of Products for the last quarter.
Also I want to show the position this Product had in the previous time period (if possible). I’ve included a small screenshot of how I would like this report to show:
[attachment=0]Capture.JPG[/attachment]
This report will be part (subreport) of a larger report (Jasper iReport). What I would like to know is if someone has any usefull tips on how to get the correct data to generate this report.
Would you suggest on building a SQL statement to generate the report in one go, or parse all records one-by-one and create a temp. table to base the report on. Or maybe you have a much better/faster approach I haven’t even thought about.

Hi Jos,

If you have a SQL backend that supports Window Functions then you can get the data in one single query.
What SQL backend do you use/support?

Currently we only use Postgres as database

Hi Jos,

PostgreSQL supports Window Functions since 8.4 so you could use that.

Hi Robert,

Thank you for that suggestion, on first glance it does exactly what I want.
It seems to be worth looking into.