code: private Query getReportQueryByCriteriaCount(ReportQueryByCriteria aQuery)
      {
          ReportQueryByCriteria countQuery = (ReportQueryByCriteria) getQueryByCriteriaCount(aQuery);
  
          // BRJ: keep the original columns to build the Join
          countQuery.setJoinAttributes(aQuery.getAttributes());
  
          // BRJ: we have to preserve groupby information
          Iterator iter = aQuery.getGroupBy().iterator();
          while(iter.hasNext())
          {
              countQuery.addGroupBy((FieldHelper) iter.next());
          }
  
          return countQuery;
      }
docs: Create a Count-Query for ReportQueryByCriteria