Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in Salesforce by (11.9k points)

Is there a way (without creating a formula datevalue(datetime) field) to aggregate a SOQL query on the date portion of a DateTime field? For example, I'd like to do something like:

select datevalue(datetimeField), count(Id) from object__c group by datevalue(datetimeField)

1 Answer

0 votes
by (32.1k points)

You can't really group on a DateTime field instantly, there are plenty of date/time functions for aggregates. By which you can group by section of the DateTime value. For example, here's a query that'll show the number of account records created on each date.

select day_only(createdDate) createdonDate, 

    count(createdDate) numCreated 

    from account 

    group by day_only(createdDate) 

    order by day_only(createdDate) desc

Note: day_only() is used to return the date part of a DateTime field.

Browse Categories