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

Author  Topic 

markj11
Starting Member

17 Posts

Posted - 2008-01-17 : 09:34:53
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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-17 : 09:41:40
Try:-
SELECT ROW_NUMBER() OVER(PARTITION BY Customer ORDER BY Customer) AS LineNo,
Customer
FROM Table
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-17 : 09:43:57
Select customer,row_number() over(partition by customer order by customer) as line_no from table
Also see what you can do with row_number() function
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/multipurpose-row-number-function.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-17 : 09:44:28


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

markj11
Starting Member

17 Posts

Posted - 2008-01-17 : 09:56:46
Thanks, works great. I wouldn't exactly call myself a rookie but I've never even heard of row_number, over, or partition.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-18 : 01:00:59
quote:
Originally posted by markj11

Thanks, works great. I wouldn't exactly call myself a rookie but I've never even heard of row_number, over, or partition.


Then read the link I posted

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -