MSSQL Query - Get the last 'x' rows

Hitman
Soldato
Joined
25 Feb 2004
Posts
2,837
Hi,

Having some problems with this. I've got a database which holds 2 pieces of information: Date and Count. I'm using this data to plot a graph, but I need it to plot only the last 25 entries.

Now, I could do this:
SELECT TOP 25 date, count FROM Table ORDER BY date DESC

...but this would flip the results round, so the last entry would be the first on the graph (not what I want!).

Is there any way to either have PHP flip the results around, or use a query to select the last 25 results?

Cheers
 
The problem is the final Order By - the the date field is not referenced to any table through the subquery, the below should work:

Code:
SELECT TOP 25 date, count FROM 
(SELECT TOP 25 date, count FROM table ORDER BY date DESC) AS SubQuery
ORDER BY SubQuery.date ASC

Absolutely perfect! Works perfectly. Now my graphs don't look back to front :D

Cheers!
 
Back
Top Bottom