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)
 Counter Problem

Author  Topic 

harrisw48
Starting Member

34 Posts

Posted - 2008-10-20 : 09:02:49
Im trying to create a counter for a group of records and then resets back to 1 once the next group changes (see the counter filed below)

urn counter
a1234 1
a1234 2
b1234 1
c1234 1
d1234 1
d1234 2

I cant think of a way to do this. Can anyone help?

jhocutt
Constraint Violating Yak Guru

385 Posts

Posted - 2008-10-20 : 09:29:59
[code]
create table #tmp (
myid int identity(1,1),
urn varchar(10)
)

insert into #tmp (urn)
select 'a1234'
union all select 'a1234'
union all select 'b1234'
union all select 'c1234'
union all select 'd1234'
union all select 'd1234'


select
urn,
(select count(*) from #tmp as e2 where e2.myid<= e1.myid AND e2.urn = e1.urn ) as counter
from #tmp as e1
order by urn

drop table #tmp
[/code]

"God does not play dice" -- Albert Einstein
"Not only does God play dice, but he sometimes throws them where they cannot be seen."
-- Stephen Hawking
Go to Top of Page
   

- Advertisement -