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 |
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 countera1234 1 a1234 2b1234 1c1234 1d1234 1d1234 2I 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 counterfrom #tmp as e1 order by urndrop 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 |
|
|
|
|
|