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
 SQL Server Development (2000)
 Adding a count

Author  Topic 

icw
Constraint Violating Yak Guru

378 Posts

Posted - 2010-03-29 : 01:00:08
Hi I have a table with potentially several rows for each unique accountno (i.e. each company). I would like to add a column that is a row count for each accountno no.
e.g. Table looks like this

Accountno date
abc123 01/01/98
abc123 23/03/03
def345 21/01/91
abcdef 30/01/08
def345 12/06/09
ghi678 13/02/03

I would like to select it and add the rowcount so it looks like this

Row Accountno date
1 abc123 01/01/98
2 abc123 23/03/03
3 abcdef 30/01/08
1 def345 21/01/91
2 def345 12/06/09
1 ghi678 13/02/03

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-29 : 01:24:33
[code]
SELECT (SELECT COUNT(*) FROM Table WHERE Accountno=t.Accountno AND date < t.date) + 1 AS Row,
Accountno,
date
FROM table t
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

icw
Constraint Violating Yak Guru

378 Posts

Posted - 2010-03-29 : 01:34:48
That's Great
thanks a lot Visakh16
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-29 : 02:02:42
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -