TOP command and what it returns (help please)

MNottingham

New member
Local time
Yesterday, 19:22
Joined
May 4, 2013
Messages
6
So let me start off by saying thanks to everyone here, this forum has been a great help in the past to me when I get stuck. This time however I cannnot find the answer I seek by cruising around here and googling.

Currently I have a table that is recording inspections. I need to see the last 2 inspections for each piece of equipment based on the date of the inspection. I have a query using the top function that returns two dates for each piece of equipment however they are the oldest two dates not the most recent.

The way I understand the TOP function is that it will change what it retrieves based on ASC/DESC order value, however if I change mine it does nothing, and continues to return the same two (oldest dates).

Here is the code Im working with:

SELECT [EQUIPDATA].EQID, [EQUIPDATA].[RATINGAFTER], [EQUIPDATA].INSPDATE
FROM [EQUIPDATA]
GROUP BY [EQUIPDATA].EQID, [EQUIPDATA].[RATINGAFTER], [EQUIPDATA].INSPDATE
HAVING ((([EQUIPDATA].INSPDATE) In (SELECT TOP 2 T.INSPDATE FROM [EQUIPDATA] T WHERE T.EQID = [EQUIPDATA].EQID ORDER BY T.INSPDATE)))
ORDER BY [EQUIPDATA].EQID;

All I need is to swap the dates that it retrieves from the oldest ones (what it does now) to the most recent.

Thanks for any help you can provide!
 
Add DESC to your ordering:


...ORDER BY T.INSPDATE DESC)...
 

Users who are viewing this thread

Back
Top Bottom