• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

SQL Question: Rank or Number columns

I have a SQL query something like this:

select CustomerID, Columna, Columnb, Columnc    
order by Columnc

and I want to be able to add a new smallint column at the end that will number CustomerID records in order.
So for example:
 
111555 Columna Columnb Columnc   1
111555 Columna Columnb Columnc   2
111555 Columna Columnb Columnc   3

How can I do this?
0
vd
Asked:
vd
1 Solution
 
Scott PletcherSenior DBACommented:
Unfortunately SQL Server doesn't directly provide that capability in any way.  You have to use "tricks"/subqueries to generate such a sequence, especially if it needs "reset".

If you want all rows numbered in sequence, without ever resetting the sequence, you can SELECT ... INTO a temp table and include an IDENTITY() function to generate a sequential number, then do the final SELECT from that temp table.
0
 
vdAuthor Commented:
Yes I'm using a temp table.  I thought about the Identity column before, but maybe I should revisit that idea.
0
 
Brian CroweCommented:
SELECT *, IDENTITY(int, 1, 1) AS RowNum INTO #temp FROM myTable

FYI...SQL2005 will have a ROW_NUMBER function
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
geotigerCommented:

the identity method only works in the insertion, i.e., you need to order them at the time you insert the sorted records into the temp table. It will not work during your selection, i.e., the sequence number in the identity column will not change if you change your sort criteria in a new selection.

Oracle has an internal column called rownum, it will provide sequence number at time of selection such as

select CustomerID, Columna, Columnb, Columnc , rownum
from your_table
order by Columnc ;


0
 
vdAuthor Commented:
OK, I have a table that uses the Identity column mentioned above.  Now, how do I use that  to get a list of the
lowest ranking (or rownum)  and highest ranking for
each CustomerID ?  
I'll add points if this can be answered. This probably should have been my original question !    
0
 
Scott PletcherSenior DBACommented:
Assuming the identity columns is named "ident" (for example), please try this:

select CustomerID, Columna, Columnb, Columnc,
    (SELECT COUNT(*)
    FROM ... t2
    WHERE t2.CustomerID = t1.CustomerID AND t2.ident <= t1.ident) AS [Cust Sequence]
from ... t1
order by Columnc
0
 
Scott PletcherSenior DBACommented:
It would be *very helpful* to the subquery if there were a unique index on (CustomerId, Identity) [together, in that order].
0
 
vdAuthor Commented:
Thanks very much! This has been very helpful.
It turns out I didn't need the identity column, but in working with it I figured out how I could handle this.
Basically, I create two temp tables which I Join
along with using the  Min and Max functions.
Thanks.
0
 
Scott PletcherSenior DBACommented:
Great ... glad you found a proper solution!
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now