Solved

Creating an item sequence by account number

Posted on 2013-11-27
6
313 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 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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

630 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