Solved

Creating an item sequence by account number

Posted on 2013-11-27
6
309 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remove all hidden metadata properties of MS .Docx Files 7 38
deduplicating based on criteria 2 21
DSN-LESS connection to MS Access database 6 31
IIF help, YN field 7 22
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

803 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