views:

109

answers:

1

I have a dataset that looks like this:

Account  Cost Centre TransNo
aaa      111         43443
aaa      111         32112
aaa      111         43211
aaa      112         32232
aaa      113         56544
bbb      222         43222
bbb      222         98332
ccc      111         88778

I need a column added that is a counter of the number of rows that relate to that Account/Cost Centre combination:

Account  Cost Centre TransNo  rCounter
aaa      111         43443      1
aaa      111         32112      2
aaa      111         43211      3
aaa      112         32232      1
aaa      112         56544      2
bbb      222         43222      1
bbb      222         98332      2
ccc      111         88778      1

Is this possible to do in MSAccess using SQL? and how would I go about it (ie what would be the SQL script I would need to write)?

Thanks in advance.

A: 

Something like:

SELECT a.Account, a.[Cost Centre], a.TransNo, (SELECT Count(*) 
   FROM table4  b
   WHERE b.Account=a.Account 
   AND b.[Cost Centre]=a.[Cost Centre] 
   AND b.TransNo<=a.TransNo) AS AccountNo
FROM Table4 AS a
ORDER BY a.Account, a.[Cost Centre], a.TransNo;
Remou
Sweet! The only issue I can see is that this is going to be slow for large data sets. I ran it on a table with 131K records and it was pretty awful. Restricting to one of the first two columns is likely the only way to make it usable.
David-W-Fenton
Awesome! Works a treat. Thanks a million Remou
Withnail