Click here to Skip to main content
15,878,230 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
ProductID   Name                   LocationID   Quantity SeqNo
----------- ---------------------- ------------ -------- ----
494         Paint - Silver         3            49       1
495         Paint - Blue           3            49       1
493         Paint - Red            3            51       3
496         Paint - Yellow         3            56       4

here....by using Rank() function seqNo column was generated...but after 1....i want 2....and then 3....but it skips the numbers....(grouping is on LoationID and Quantity).

I Want Out Like Below
ProductID   Name                   LocationID   Quantity SeqNo
----------- ---------------------- ------------ -------- ----
494         Paint - Silver         3            49       1
495         Paint - Blue           3            49       1
493         Paint - Red            3            51       2
496         Paint - Yellow         3            56       3

how should i do that....
Posted
Updated 14-Apr-14 1:46am
v2

You can use Dense Rank
[^]

According to MSDN:

Returns the rank of rows within the partition of a result set, without any gaps in the ranking. The rank of a row is one plus the number of distinct ranks that come before the row in question.

VB
SELECT
    ProductID,
    Name,
    LocationID,
    Quantity,
    DENSE_RANK() OVER (ORDER BY LocationID, Quantity ASC) AS Rank
FROM
    Products
ORDER
    BY LocationID, Quantity;
 
Share this answer
 
v3
try query given below in comments...
Happy Coding!
:)
 
Share this answer
 
Comments
Aarti Meswania 14-Apr-14 7:58am    
WITH a as
(
SELECT 49 as qty, 'a' as nm UNION ALL
SELECT 51 as qty, 'b' as nm UNION ALL
SELECT 49 as qty, 'c' as nm UNION ALL
SELECT 50 as qty, 'd' as nm
)
SELECT qty,nm,row_number() OVER (partition BY srno order by qty,nm) from
(
SELECT qty,nm, row_number() OVER (partition BY qty order by nm) srno from a
) as temp
order by qty,nm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900