function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RahulRahul 

HELLO FRIENDS, I am looking for a query for sum of premium period amount for unique person IDs

Person object is parent object and premium period is child. There are 1 million premium period records for the year 2022. There is a field premium period amount on premium period object. Iam trying to get the sum of premium period amount for all the unique person IDs. Person ID is the field on person object.

I tried but I am not able to get it. Please, I need your help. Waiting for your reply. Thanks in advance.
SubratSubrat (Salesforce Developers) 
Hello Rahul ,

I would request you to go through this discussion which has dealt with querying records more than 500k ,

Discussion -> https://salesforce.stackexchange.com/questions/50037/using-aggregate-soql-with-large-amounts-of-data-500k-records

Let me know further on this .

Thank you.
Arun Kumar 1141Arun Kumar 1141
Hi Rahul,

you can use this, hope this will help.
List<Premium_period__c> prePeriod = [SELECT Premium_period_amount__c, Person__r.Id FROM Premium_period__c WHERE CreatedDate >= 2022-01-01T00:00:00Z AND CreatedDate < 2023-06-16T00:00:00Z];

Map<Id, Decimal> personAmtMap = new Map<Id,Decimal>();
for(Premium_period__c periodIns : prePeriod)
{
	if(personAmtMap.containsKey(periodIns.Person__r.Id)){
	    personAmtMap.put(periodIns.Person__r.Id, personAmtMap.get(periodIns.Person__r.Id)+ periodIns.Premium_period_amount__c);

	}
	else{
	personAmtMap.put(periodIns.Person__r.Id, periodIns.Premium_period_amount__c);
	}
}

for(Id personIds : personAmtMap.keySet())
{
	System.debug('Id:' +personIds+ ', Period Amount:'+personAmtMap.get(personIds));
}

please mark as the best answer if it's  help you.
Thanks