UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Numbering Rows in a Query    
 
   
ktrnhbr
post Nov 2 2005, 01:02 AM
Post #1

New Member
Posts: 9



I am looking for help numbering the rows of a query sequentially. I found some code posted on an old thread (in spanish) but I could not get it to work. Any assistance would be greatly appreciated.

Thank you.
Go to the top of the page
 
+
mishej
post Nov 2 2005, 01:27 AM
Post #2

Retired Moderator
Posts: 11,289
From: Milwaukee, WI



Do you have a field that determines the sort order? A date or serial number or perhaps text that needs to be sorted alphabetically?

If you have something like that you can use a counting query. The sql looks like this:
CODE
SELECT p.KeyPct, (SELECT COUNT(*) FROM tblMiscPct WHERE KeyPct >= p.KeyPct) AS Rank
FROM tblMiscPct AS p
ORDER BY p.KeyPct DESC;

Here we rank a set of records by a percent field. By counting records with an equal or greater value than the current record we come up with a "ranking" or "row" number.

There can be ties, so the ORDER BY becomes more important.

If you post more info on your needs someone will provide more exact help.
Go to the top of the page
 
+
ktrnhbr
post Nov 2 2005, 02:24 AM
Post #3

New Member
Posts: 9



I am sorting the records by a class field (classes are a - zz) where there will be more than one entry in each class and I want to get a numbered list without any duplicate numbers. Will the code you provided create duplicate numbers (rankings) under these conditions?
Go to the top of the page
 
+
mishej
post Nov 2 2005, 02:32 AM
Post #4

Retired Moderator
Posts: 11,289
From: Milwaukee, WI



Yes if you use just the class field. If there are more than one record for a class how do you determine which one is numbered first sequentially? Must be another field you use to "break the tie". Otherwise, how do you order them?
Go to the top of the page
 
+
ktrnhbr
post Nov 2 2005, 02:50 AM
Post #5

New Member
Posts: 9



Yes, after class I use an assigned number (PlateNum) to break the tie. Pardon my ignorance, but I can not seem to get this to work. I get the same number for each record.
Go to the top of the page
 
+
mishej
post Nov 2 2005, 02:56 AM
Post #6

Retired Moderator
Posts: 11,289
From: Milwaukee, WI



Based on what you've shared the SQL might look like this:
CODE
SELECT
  t.class
, t.PlateNum
, (SELECT COUNT(*) FROM someTableYouAreUsing WHERE class >= t.class and PlateNum >= t.PlateNum) AS Rank
FROM
  someTableYouAreUsing AS t
ORDER BY
  t.class, t.PlateNum;
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 22nd May 2013 - 04:02 AM