The OnPrint() event of the Detail section executes on each record and therefore can be used to increment some value based on your criteria.
If you aren't concerned with preserving the data and only want to count all distinct combinations of A,B,C,D then the aggregate function count() could be...