Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Queries / November 2005

Tip: Looking for answers? Try searching our database.

Numbering Rows

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tjh - 29 Nov 2005 16:25 GMT
Hello,

Is it possible to number rows in a query up to a certain number, then begin
the numbering sequence again. For example: The original table currently looks
something like.

TitleName

Jim
Jan
John
Al
Frank
Sam
Kate
Sally
Susie
Amy
Kevin                        
Phil                          

I would like the Query to number each row up to 3 then restart the numbering
sequence to display an output similar to:

TitleName             NumberSeq

Jim                           1
Jan                           2
John                         3
Al                             1
Frank                        2
Sam                         3
Kate                         1
Sally                         2
Susie                        3
Amy                         1
Kevin                        2
Phil                          3

Please let me know if you need more information.

Thank You for your help,
Chris2 - 29 Nov 2005 20:16 GMT
> Hello,
>
> Is it possible to number rows in a query up to a certain number, then begin
> the numbering sequence again. For example: The original table currently looks
> something like.

<snip>

> I would like the Query to number each row up to 3 then restart the numbering
> sequence to display an output similar to:

<snip>

> Please let me know if you need more information.
>
> Thank You for your help,

tjh,

Tables:

Please forgive the dates appended to the table names.

Note: I specified a new column "TitleID" because according to the
desired results, the order of TitleName must be presevered, and
without some other column there to preserve that order, I don't
think it's going to work.

CREATE TABLE Titles_20051129_1
(TitleID            AUTOINCREMENT
,TitleName          TEXT(5)
,CONSTRAINT pk_Titles_20051129_1
           PRIMARY KEY (TitleID)
)

Sample Data:

1, Jim
2, Jan
3, John
4, Al
5, Frank
6, Sam
7, Kate
8, Sally
9, Susie
10, Amy
11, Kevin
12, Phil

Query:

SELECT T1.TitleName
     ,(SELECT SWITCH(COUNT(T1.TitleID) MOD 3 = 1, 1
                    ,COUNT(T1.TitleID) MOD 3 = 2, 2
                    ,COUNT(T1.TitleID) MOD 3 = 0, 3)
         FROM Titles_20051129_1 AS T01
        WHERE T01.TitleID <= T1.TitleID)
      AS NumberSeq
 FROM Titles_20051129_1 AS T1

Results:

TitleName, NumberSeq

Jim,       1
Jan,       2
John,      3
Al,        1
Frank,     2
Sam,       3
Kate,      1
Sally,     2
Susie,     3
Amy,       1
Kevin,     2
Phil,      3

That appears to be a copy of the desired results.

If you don't have, or can't add, a column equivalent to TitleID,
then you'll have to order TitleName.

Query 2:

SELECT T1.TitleName
     ,(SELECT SWITCH(COUNT(T1.TitleName) MOD 3 = 1, 1
                    ,COUNT(T1.TitleName) MOD 3 = 2, 2
                    ,COUNT(T1.TitleName) MOD 3 = 0, 3)
         FROM Titles_20051129_1 AS T01
        WHERE T01.TitleName <= T1.TitleName)
      AS NumberSeq
 FROM (SELECT TOP 100 PERCENT
              T02.TitleName
         FROM Titles_20051129_1 AS T02
     ORDER BY T02.TitleName) AS T1

Results 2:

TitleName, NumberSeq

Al,        1
Amy,       2
Frank,     3
Jan,       1
Jim,       2
John,      3
Kate,      1
Kevin,     2
Phil,      3
Sally,     1
Sam,       2
Susie,     3

Of course, this doesn't quite match the original desired results.

Sincerely,

Chris O.
John Nurick - 29 Nov 2005 22:26 GMT
Can be done. This example assumes that ID is the primary key and
demonstrates

sequential numbering of records 0123456789  (RecNum)
repeated numbering 111122223333    (SlowStep)
cyclic numbering 123412341234   (Cyclic)

SELECT A.ID,

(SELECT COUNT(ID) FROM [TheTable] AS B WHERE B.ID < A.ID) AS RecNum,

Int(((SELECT COUNT(ID) FROM [TheTable] AS C WHERE C.ID < A.ID) Mod
52)/4)+1 AS SlowStep,

Int((SELECT COUNT(ID) FROM [TheTable] AS C WHERE C.ID < A.ID) Mod 4)+1
AS Cyclic

FROM [TheTable] AS A
ORDER BY A.ID;

>Hello,
>
[quoted text clipped - 38 lines]
>
>Thank You for your help,

--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.