Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ROW_Count() To Start Over Based On Order

Create Table #Test (
    ID Int Primary Key Identity,
    Category VarChar(100)
    )

Insert into #Test
(Category)
Values
('Banana'),
('Banana'),
('Banana'),
('Banana'),
('Banana'),
('Banana'),
('Strawberry'),
('Strawberry'),
('Strawberry'),
('Banana'),
('Banana')

Select
     *
    ,ROW_NUMBER() Over (Partition by Category order by ID) as RowNum

From #Test

Order by ID

So this script returns this:

ID  Category    RowNum
1   Banana      1
2   Banana      2
3   Banana      3
4   Banana      4
5   Banana      5
6   Banana      6
7   Strawberry  1
8   Strawberry  2
9   Strawberry  3
10  Banana      7
11  Banana      8

Which makes perfect sense, except I want it to return this:

ID  Category    RowNum
1   Banana      1
2   Banana      2
3   Banana      3
4   Banana      4
5   Banana      5
6   Banana      6
7   Strawberry  1
8   Strawberry  2
9   Strawberry  3
10  Banana      1
11  Banana      2

I want it to restart the count when it hits a new set of Banana. Obviously my data is not really bananas, but it makes it easy to visualize.

This recurrence of bananas is considered to be new, so we want to start counting from one when we see this. I've been racking my brain and can't think of a good way to do this. I understand why it is not working but can't think of a way to make it work. Any advice on the best way to do this?

like image 691
Warpnow Avatar asked Aug 10 '15 22:08

Warpnow


People also ask

What is ROW_NUMBER () over partition by?

The Row_Number function is used to provide consecutive numbering of the rows in the result by the order selected in the OVER clause for each partition specified in the OVER clause. It will assign the value 1 for the first row and increase the number of the subsequent rows.

Does ROW_NUMBER require order by?

Because the ROW_NUMBER function requires an ORDER BY clause, the ROW_NUMBER function specifies ORDER BY (SELECT 1) to return the rows in the order in which they are stored in the specified table and sequentially number them, starting from 1.

How do I start a row number from a specific value in SQL?

You can only add to it. From Oracle documentation: "ROW_NUMBER is an analytic function. It assigns a unique number to each row to which it is applied (either each row in the partition or each row returned by the query), in the ordered sequence of rows specified in the order_by_clause, beginning with 1."

What is ROW_NUMBER () in SQL?

ROW_NUMBER numbers all rows sequentially (for example 1, 2, 3, 4, 5). RANK provides the same numeric value for ties (for example 1, 2, 2, 4, 5). ROW_NUMBER is a temporary value calculated when the query is run. To persist numbers in a table, see IDENTITY Property and SEQUENCE.


1 Answers

There are several different ways to approach this. One method is the difference of row_number() approach. This method will identify groups of adjacent categories that are the same:

Select t.*,
       row_number() over (partition by grp, category order by id) as rownum
From (select t.*, 
             (row_number() over (order by id) -
              row_number() over (partition by category order by id)
             ) as grp
      from #Test t
     ) t
Order by ID;

You can also figure out the groupings using lag(), but this will work in SQL Server 2005 and 2008 as well as more recent versions.

like image 145
Gordon Linoff Avatar answered Oct 06 '22 01:10

Gordon Linoff