I cannot understand why I am receiving the following error:
Too many query rows: 50001
The first two queries work perfectly when using COUNT( id), but the last two Contacts_Reached_TR_120_Query and Accounts_Reached_TR_120_Query receive this error. Is there something wrong with using COUNT_DISTINCT. I also would like to note that the Aggregate Result is only returning six rows.
String Contacts_Created_TR_120_Query = 'SELECT CreatedByID SDR_ID, COUNT( id ) result FROM Contact Where CreatedDate = LAST_N_DAYS:120 AND CreatedById in :ListOfIDs GROUP BY CreatedById';
DynamicBatchApexAR batch1_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Contacts_Created_TR_120__c', Contacts_Created_TR_120_Query, SDR_IDs);
String Accounts_Created_TR_120_Query = 'SELECT CreatedByID SDR_ID, COUNT( id ) result FROM Account Where CreatedDate = LAST_N_DAYS:120 AND CreatedById in :ListOfIDs GROUP BY CreatedById';
DynamicBatchApexAR batch2_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Accounts_Created_TR_120__c', Accounts_Created_TR_120_Query, SDR_IDs);
String Contacts_Reached_TR_120_Query = 'SELECT Ownerid SDR_ID, COUNT_DISTINCT( whoid ) result FROM Task Where ActivityDate = LAST_N_DAYS:120 AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch3_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Contacts_Reached_TR_120__c', Contacts_Reached_TR_120_Query, SDR_IDs);
String Accounts_Reached_TR_120_Query = 'SELECT Ownerid SDR_ID, COUNT_DISTINCT( accountid ) result FROM Task Where ActivityDate = LAST_N_DAYS:120 AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch4_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Accounts_Reached_TR_120__c', Accounts_Reached_TR_120_Query, SDR_IDs);