Solved

Creating an item sequence by account number

Posted on 2013-11-27
6
311 Views
Last Modified: 2013-11-27
I have a table that contains the detail records of an account. Each account can have up to 99 pieces of equipment associated with it. The table contain 125,000 records. I need to create a sequence number of reach item within the account. If I have 83 items and my account number is 123456, then I need to number the items from 1 to 83. If the next account, 123457, has 28 items, I need to number them 1 to 28. And so on. . .

What is the best method to create such sequencing in an existing data table?
0
Comment
Question by:ergenbgr
  • 4
  • 2
6 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 39680440
Are you doing this to display item number/sequence on forms and reports?

I'd do this by query rather than storing the item number in the table.

If you have an autonumber field in your table (assuming here that it is called ID), you can query the data with a sequence number like this:


Select a.*, (SELECT Count(b.ID) FROM YourTable b WHERE b.ID <= a.ID AND b.[AccountNumber] = a.[AccountNumber]) AS [SequenceByAccountNumber]
FROM YourTable a
ORDER BY a.[AccountNumber],  a.ID

Open in new window



You can also convert that to an UPDATE query by right-clicking the query window, selecting Query Type --> Update Query if you really need to store the sequence... but I would simply get it in a query like this on the fly, and display it when needed.
0
 

Author Comment

by:ergenbgr
ID: 39680513
Sounds like a great way to do it. I will try it an see how it works. One additional caveat is that I need to start the sequence at 900. I presume I can just factor that in the query by adding 900 to the result. Would you agree?
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 39680549
Exactly...  and also change the <= to simply <.  My example above starts at 1 (so adding 900 would be 901 as a starting point).  If you use  < for the comparison, the count will start at 0 (900 if you add 900)

Try this:

Select a.*, (SELECT Count(b.ID) + 900 FROM YourTable b WHERE b.ID < a.ID AND b.[AccountNumber] = a.[AccountNumber]) AS [SequenceByAccountNumber]
FROM YourTable a
ORDER BY a.[AccountNumber],  a.ID

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ergenbgr
ID: 39680796
Thanks mbizuo! Great solution.
0
 

Author Comment

by:ergenbgr
ID: 39680812
I've requested that this question be closed as follows:

Accepted answer: 0 points for ergenbgr's comment #a39680796

for the following reason:

Simple, yet effective, solution.
0
 

Author Closing Comment

by:ergenbgr
ID: 39680813
Simple, but effective. My kind of solution.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel printing an invoice header over two sheets 3 30
exchange, office 365 13 37
Excel + % to Goal when number is negative 21 26
need counting specific data on sheet 26 25
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question