For SQL server 2008 R2
It seems challenging to come up with a single query to the following:
Example given columns a & b:
a | b
-------------
0 | 2000
1 | 2001
1 | 2002
1 | 2003
2 | 2004
3 | 2005
1 | 2006
1 | 2007
4 | 2008
1 | 2009
Goal: Mark rows with repeated column a and give them unique number taking into account other values in between. Result should be in column c. Note the most difficult part here is to populate column c with 2 & 5 & 7.
a | b | c
-------------
0 | 2000 | 1
1 | 2001 | 2
1 | 2002 | 2
1 | 2003 | 2
2 | 2004 | 3
3 | 2005 | 4
1 | 2006 | 5
1 | 2007 | 5
4 | 2008 | 6
1 | 2009 | 7
Best Answer
This is a gaps-and-islands problem. One (of the many) ways to solve it (this requires 2012+ versions):
This should work in 2005 and above: