Solved

Calculate auto sequence number for grouped records in query

Posted on 2013-01-28
2
416 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

840 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