Search code examples
sqlms-accessrowcount

MSAccess: Ranking rows based upon column criteria


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.


Solution

  • 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;