Wrox Programmer Forums
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 16th, 2003, 10:53 AM
Registered User
 
Join Date: Oct 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Incremental record counter

Anyone here know how to do an incremental record count on grouped records? e.g.,
Key Count
A 1
A 2
A 3
B 1
B 2
B 3
B 4
B 5

Thanks in advance,
Peter
 
Old October 16th, 2003, 11:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

When you show, for example, these three rows:
Code:
A    1
A    2
A    3
How did you decide that the "A"'s were in that order? In other words, how do you distingush one "A" from another and order them the way you did? For example, what is it about the "A" in row 2 that placed it there?

I'm not trying to be flip here, but you have attributed an ordering to this data where none is apparent to me. Remember that the rows in a table are a set, and by definition, a set is unordered. Without some reliable way to distingush one row from another and thus impose some ordering on the data, you can't count it to assign an ordinal value to a column. Hint: the concept of physical order does not apply in a relational database - even Access.

Now suppose you had another column in the table which could be used to order the data. Let's suppose there was an 'EntryDate' date datatype column in the table. Now we can assign a value by dividing the table into subsets where each subset contains all the rows whose date value is less than 'this' row. We use a correlated subquery:
Code:
SELECT T1.key, 
    (SELECT COUNT(*) FROM yourtable T2 WHERE T1.key=T2.key AND T2.EntryDate<=T1.EntryDate) as TheCount
    FROM yourtable T1
ORDER BY T1.key, TheCount
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old October 16th, 2003, 01:18 PM
Registered User
 
Join Date: Oct 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jeff,
  Understood...I'm not trying to order the data, just apply a count based on how many of records of each key value exists in this table. "A" exists 5 times so the first "A" record gets a 1, the 2nd record gets a 2, the 3rd "A" records gets a 3. For example, a purchase order has one record in a key table and five details lines in a detail table. Each detail line needs the purchase order number and the 5 numbered detail lines.
PurchaseOrder(key) LineNbr(count)
A 1
A 2
A 3
A 4
A 5

How do I get Access to apply the line number count?

Hope I explained myself better with this example.
Thanks,
Peter
 
Old October 16th, 2003, 02:14 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You're missing my point.

When you say "the first "A" record gets a 1, the 2nd record gets a 2" and so on, how do you define "first"? What makes something "2nd"?

This is not merely semantics. The concepts of "first", "2nd", "third", etc have a name - these are called "ordinal numbers" and as the name implies this means that the numbering is based on some sort of ordering.

The query I gave you will number the rows based on an ordering which I invented, i.e. some date.

What is the basis of your ordering?

Now, in the case of your detail lines, you would be better served by assigning something that distinguishes one detail line from another at the time the lines were entered. In other words, you probably should have assigned the line number as each line is entered. Thus, the key in your detail table should consist of the purchase order number plus some identifier like an assigned line number.

What is the key for your detail table now? (If you used an autonumber ID column, you can use that in the query I posted instead of the date I assumed. - you need something to distinguish one detail line from another for a given purchase order - something on which to base "firstness", "secondness" etc. Anything that can be ordered will do.




Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old February 27th, 2005, 08:41 AM
Registered User
 
Join Date: Feb 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Did you ever get an answer to your incremental numbering.
I know it was a long time ago. I have the same problem so
if you can help out.

Michael





Similar Threads
Thread Thread Starter Forum Replies Last Post
Incremental Loads Johnslg BOOK: Expert SQL Server 2005 Integration Services ISBN: 978-0-470-13411-5 0 August 29th, 2007 07:13 AM
Incremental Form Validation Example Richard Frainier BOOK: Professional Ajax ISBN: 978-0-471-77778-6 2 April 22nd, 2006 03:58 PM
Incremental ETL KelMan BOOK: Professional SQL Server 2005 Integration Services ISBN: 0-7645-8435-9 1 February 27th, 2006 09:01 PM
Incremental dates in query lizhaskin VB Databases Basics 0 October 6th, 2005 02:21 AM
Incremental Search karjagis Classic ASP Basics 2 June 11th, 2004 03:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.