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
Hitesh Khatri 13Hitesh Khatri 13 

Merge two case querries in one.

Hi All is it posssible to write single query to get records filter by date and sum of a field for records greater than equals to the date as newsum and oldSum for the records lesser than the date.

Consider below code for help;:

AggregateResult[] newForecastAmount = [SELECT User__c,sum(Forecast_AMT__c) newAmount FROM Forecast__c WHERE Creation_Date__c >= startDate GROUP BY User__c];


AggregateResult[] newForecastAmount = [SELECT User__c,sum(Forecast_AMT__c) oldAmount FROM Forecast__c WHERE Creation_Date__c > startDate GROUP BY User__c];

Please let me know ASAP.

Thanks

Regards

Hitesh Khatri

Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri
It's not possible at all possible in SOQL
David @ ConfigeroDavid @ Configero
You have the option to write one SOQL query, and perform the math sum in apex by looping all of the records, then you can calculate both amounts.  Otherwise because your SOQL statements are looking for two different criteria, you only have these two options.
  • Use two SOQL statements
  • Use one SOQL statement to grab records matching both criteria (Creation_Date__c > startDate), then have conditional logic looping each record to see if it matches > or >= and add it to the appropriate sum accordingly.