Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Please HELP - adding counter

Author  Topic 

zoe2003
Starting Member

17 Posts

Posted - 2012-11-07 : 07:13:57
Hi,

I need to add a counter for each change in 2 columns like following:
this is the original table :
column1 column2
1 2
1 2
1 3
1 4
1 5
2 10
2 11
2 12
2 15
2 20

and I want a 3rd column like following:
column1 column2 counter
1 2 1
1 2 2
1 3 3
1 4 4
1 5 5
2 10 1
2 11 2
2 12 3
2 15 4
2 20 5


Thanks,
Z.

stepson
Aged Yak Warrior

545 Posts

Posted - 2012-11-07 : 07:22:03
select *
,ROW_NUMBER () OVER ( PARTITION BY column1 order by column1 ) as column3
from A

What you mean when You say "for each change in 2 columns" ?

the exemple shown by you , only the change in column 1
Go to Top of Page

zoe2003
Starting Member

17 Posts

Posted - 2012-11-07 : 07:32:00
Thanks a lot !
This is what I needed.
Go to Top of Page

stepson
Aged Yak Warrior

545 Posts

Posted - 2012-11-07 : 07:45:38
w. welcome
Go to Top of Page
   

- Advertisement -