Last transaction:
SELECT A.CNo, A.Date, A.Balance
FROM yourTable AS A INNER JOIN (
SELECT CNo, Max([Date]) AS LastDate FROM yourTable GROUP BY CNo
) AS L ON A.CNo = L.CNo AND A.Date = L.LastDate
Second last transaction:
SELECT A.CNo, A.Date, A.Balance
FROM yourTable AS A INNER JOIN (
SELECT B.CNo, Min(B.Date) AS theDate FROM yourTable AS B WHERE B.Date In
(SELECT TOP 2 [Date] FROM yourTable WHERE CNo=B.CNo ORDER BY 1 DESC)
GROUP BY B.CNo
) AS L ON A.CNo = L.CNo AND A.Date = L.theDate
Last and Second last transaction:
SELECT A.CNo, A.Date, A.Balance
FROM yourTable AS A
WHERE A.Date In (SELECT TOP 2 [Date] FROM yourTable WHERE CNo=A.CNo ORDER BY 1 DESC)
Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
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.