Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Creating an item sequence by account number

Posted on 2013-11-27
6
Medium Priority
?
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Microsoft has changed the look and feel of Azure AD and Microsoft account sign-in pages so that you will have a more unified look and feel when moving between the two interfaces.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

660 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