Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a sequential/ranking number with no code

getting an incremental value

Create a sequential/ranking number with no code

by  dhookom  Posted    (Edited  )
You can create a query that numbers/ranks records within groups. For instance the Orders table in Northwind has fields:
[tt][color #204A87]
OrderID unique autonumber
CustomerID
OrderDate[/color]
[/tt]
Assuming you want to display the records by customer with each record numbered from 1 to whatever based on the OrderDate. Each different customer would begin numbering back at 1.

The SQL view is:
[code SQL]SELECT Orders.OrderID, Orders.CustomerID, Orders.OrderDate, Count(Orders_1.OrderID) AS Rank
FROM Orders AS Orders_1 INNER JOIN Orders ON Orders_1.CustomerID = Orders.CustomerID
WHERE Orders.OrderDate>=[Orders_1].[OrderDate]
GROUP BY Orders.OrderID, Orders.CustomerID, Orders.OrderDate
ORDER BY Orders.CustomerID, Orders.OrderDate;[/code]

If you don't want to group by CustomerID, just delete the join line between the two tables:

The new SQL view is:
[code SQL]SELECT Orders.OrderID, Orders.OrderDate, Orders.CustomerID, Count(Orders_1.OrderID) AS Rank
FROM Orders AS Orders_1, Orders
WHERE Orders.OrderDate>[Orders_1].[OrderDate]
GROUP BY Orders.OrderID, Orders.OrderDate, Orders.CustomerID
ORDER BY Orders.OrderDate, Orders.CustomerID;[/code]

Orders on the same date will have the same rank value.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top