Update balanceCohorts to work with buildCohorts

This change updates balanceCohorts to work with the new changes to
buildCohorts. The code is written to target any supported DBMS.