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 |
|
S_Lakshmi
Starting Member
22 Posts |
Posted - 2008-06-09 : 12:05:17
|
Hello all,I need a help to write a query. Here is the tableDeclare @Test Table ( EName Varchar(15) )Insert into Ename Select 'a' Union allSelect 'a' Union allSelect 'a' Union allSelect 'b' Union allSelect 'c' Union allSelect 'b' Union allSelect 'd' Union allSelect 'g' Union allSelect 'g'. Now i need a result likea 0a 1a 2b 0b 1c 0d 0g 0g 1 Could any one can help me to wite this query..?ThanksLakshmi.S |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-09 : 12:11:44
|
| [code]select t.ename,s.number from ( select ename, count(*) as counting from @test group by ename) t cross join master..spt_values swhere s.type='p' and number<t.counting[/code]MadhivananFailing to plan is Planning to fail |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-06-09 : 12:17:09
|
| select ename, row_number() over(partition by ename order by ename)-1from @test a order by enameJim |
 |
|
|
S_Lakshmi
Starting Member
22 Posts |
Posted - 2008-06-09 : 12:21:32
|
| Thanks Madhivanan and Jim. Both are working !! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-09 : 13:03:21
|
quote: Originally posted by jimf select ename, row_number() over(partition by ename order by ename)-1from @test a order by enameJim
Well. I didnt see this is posted in SQL Server 2005 forum MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|