Solved

Calculate auto sequence number for grouped records in query

Posted on 2013-01-28
2
417 Views
Last Modified: 2013-01-28
I need to group records in a query and create a new column which assigns an auto-incrementing sequence number for the returned rows per group.

Using the example below, I group on ID and TYPE, sort by SEQ, and return an auto incrementing number for each group.

Example Data
ID     TYPE   SEQ   AUTOSEQ
001   M       1       1
001   N       2        1
001   N       3        2
001   M       4       2
001   N       5        3
002   M      1        1
002   M      2       2
002   N       3       1
0
Comment
Question by:Jinghui Li
2 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 38828229
try this query


SELECT Table4.ID, Table4.type, Table4.seq, (select count(*) from table4 as t where t.id=table4.id and t.type=table4.type and t.seq<=table4.seq) AS AutoSeq
FROM Table4
ORDER BY Table4.ID, Table4.seq;
0
 

Author Closing Comment

by:Jinghui Li
ID: 38828366
Brilliant!

I'm trying to wrap my head around how that actually works - but it is perfect, thanks!
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

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