The following SQL produces a dataset for all Northwind orders placed in
August 1996:
SELECT Orders.OrderID, Orders.OrderDate
FROM Orders
WHERE Orders.OrderDate Between #8/1/1996# And #8/31/1996#
The following SQL adds a sequential number to each record(starting from 1)
to each record in the previous dataset:
SELECT Orders.OrderID, Orders.OrderDate, Count(qryRecNo.RecNo) AS RecNo
FROM Orders,
(Select Distinct Orders.OrderID as RecNo from Orders WHERE
Orders.OrderDate Between #8/1/1996# And #8/31/1996# ) AS qryRecNo
WHERE (((qryRecNo.RecNo)<=[orderid])) AND
Orders.OrderDate Between #8/1/1996# And #8/31/1996#
GROUP BY Orders.OrderID, Orders.OrderDate
ORDER BY Count(qryRecNo.RecNo);
The trick used is that for each record in the Orders table, a count of the
OrderID's less than or equal to the current one, produces the sequential
number. As the first OrderID of that month is 10270, there is only one
record that is less than or equal to it, thus it receives the RecNo of 1.
The last OrderID of that month is 10294, and there are 25 other records that
are less than or equal to it.
This output is numbered by the OrderID in ascending order. This is because
the Less Than(<) symbol is used in the WHERE clause. To reverse the sort
order to Descending, change it to a Greater Than(>) symbol.