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.
Author |
Topic |
markj11
Starting Member
17 Posts |
Posted - 2008-01-25 : 09:11:11
|
How can I create a counter (line#) by group were it resets = 1 whenever a new group starts.Customer Line#A 1A 2A 3B 1C 1C 2In sql 2005 I used row_number discussed in the following post.[url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=95690[/url] |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-25 : 09:29:50
|
DO you have any other columns in your table? preferably one with unique values. |
 |
|
markj11
Starting Member
17 Posts |
Posted - 2008-01-25 : 09:45:58
|
Yes, I am doing a select into a temp table so I can say:select identity(int, 1, 1) as recno, customer, 0 as Line#into #tempfrom mytable |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-25 : 09:53:23
|
then do this:-UPDATE tSET t.Line#=tmp.SeqNoFROM #temp tINNER JOIN(SELECT recno, Customer, (SELECT COUNT(*) + 1 FROM #temp WHERE customer=c.customer AND recno < c.recno) AS SeqNoFROM #temp c)tmpON tmp.recno=t.recno |
 |
|
markj11
Starting Member
17 Posts |
Posted - 2008-01-25 : 10:06:58
|
Thank you very much. |
 |
|
|
|
|