The first query called qryLastCommentDate will return the last comment date for each Key.
SELECT tblComments.Key, Max(tblComments.Date) AS LastCommentDate
FROM tblComments
GROUP BY tblComments.Key;
The second query called qryNextToLastCommentDate will return the last and next to last comment dates:
SELECT tblComments.Key, [qryLastCommentDate].LastCommentDate, Max(tblComments.Date) AS NextToLastCommentDate
FROM tblComments INNER JOIN qryLastCommentDate ON tblComments.Key=[qryLastCommentDate].Key
WHERE (((tblComments.Date)<[LastCommentDate]))
GROUP BY tblComments.Key, [qryLastCommentDate].LastCommentDate;
But all that is returned is the top 2 key numbers
I need top 2 comments by date for each key number...
what am I missing?
''''''''''''
SELECT top 2 NPETPROD_TCOMNT.KEYNBR,NPETPROD_TCOMNT.DTCOMENT, NPETPROD_TCOMNT.SEQ
FROM NPETPROD_TCOMNT
WHERE ((NPETPROD_TCOMNT.SEQ)=1)
ORDER BY NPETPROD_TCOMNT.DTCOMENT DESC
;
I really don't get this. I do this all the time, just check @ MS and they confirm...
If a SELECT statement that includes TOP also has an ORDER BY clause, the rows to be returned are selected from the ordered result set. The entire result set is built in the specified order and the top n rows in the ordered result set are returned.
So adding TOP 2 should not change the order.
There are two ways to write error-free programs; only the third one works.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.