ItemID | NumberOfUses | LastMaint | Partition
0111 1 NULL 1
0111 1 1/1/2015 2
0111 2 1/1/2015 2
0111 3 1/1/2015 2
0111 4 1/1/2015 2
0111 5 1/1/2015 2
0111 1 4/1/2015 3
0111 2 4/1/2015 3
0111 3 4/1/2015 3
0111 1 7/1/2015 4
0111 2 7/1/2015 4
0111 3 7/1/2015 4
0111 4 7/1/2015 4
0111 5 7/1/2015 4
0111 6 7/1/2015 4
0111 7 7/1/2015 4
0111 8 7/1/2015 4
0111 9 7/1/2015 4
What I am trying to figure out is how to get the Partition column to look like the above. (I added a space between the different groups/partitions just to make it easier to look at).
I've been trying to do this using something close to the query below. I've tried just about every combination of columns in the ROW_NUMBER() OVER()
SELECT [ItemID]
,[NumberOfUses]
,[LastMaintenance]
,ROW_NUMBER() OVER (PARTITION BY ?????????
ORDER BY ???????) [Partition]
FROM [ItemsHistory]
ORDER BY [Partition]
Try DENSE_RANK:
SELECT [ItemID]
,[NumberOfUses]
,[LastMaintenance]
,DENSE_RANK() OVER (PARTITION BY [ItemId] ORDER BY [LastMaintenance]) [Partition]
FROM [ItemsHistory]
ORDER BY [ItemId], [Partition], [NumberOfUses]