[SalesForce] How to do aggregated query base on a condition

I have an 'Job' object, and I want to group and count the job records into two groups:
1. jobs that created before certain date
2. jobs that created after certain date

Can I achieve this via soql aggregated query?
Something like:

List<AggregatedResult> results = [select count(Id) From Job__c Group By CreatedDate > {certainDate}];

Best Answer

If your criteria date is fixed here is what i would do:

  1. Create new field say "Group_type" update this field based on criteria using Trigger/ workflow

  2. use this Group_type field in query for grouping
    List<AggregatedResult> results = [Select count(Id) From Job__c Group By Group_type ];

  3. This should give you same result with with one query

  4. Note that it will return both groups in different rows but you can write apex logic around it to get the data.

Related Topic