Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Row Count Reset?

Status
Not open for further replies.
Oct 17, 2006
227
HI

I have built table to which I have created an inital composite key

a,b ab
a,b ab
b,c bc
b,c bc

now I was wondering how you would do a count that I can reset on compkey

if I do SELECT row_number() over (order by COMKEY), COMKEY from table


I have ab 1, ab 2, bc 3, bc 4 but really want

ab,1
ab,2
bc,1
bc,2


Please can someone help

Many Thanks
 
Apologies its been a while since Ive used this

SELECT row_number() over (PARTITION BY COMKEY ORDER BY COMKEY ), COMKEY from table
 
You want to use the Partition By keyword. Like this:

Code:
SELECT row_number() over ([!]Partition By COMKEY[/!] order by COMKEY), COMKEY from table

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top