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 2000 Forums
 Transact-SQL (2000)
 Group by counter

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 1
A 2
A 3
B 1
C 1
C 2

In 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.
Go to Top of Page

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 #temp
from mytable
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-25 : 09:53:23
then do this:-

UPDATE t
SET t.Line#=tmp.SeqNo
FROM #temp t
INNER JOIN
(SELECT recno,
Customer,
(SELECT COUNT(*) + 1 FROM #temp WHERE customer=c.customer AND recno < c.recno) AS SeqNo
FROM #temp c)tmp
ON tmp.recno=t.recno
Go to Top of Page

markj11
Starting Member

17 Posts

Posted - 2008-01-25 : 10:06:58
Thank you very much.
Go to Top of Page
   

- Advertisement -