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
Vishal_ThoriyaVishal_Thoriya 

Is there a way to group by the date portion of a datetime field in SOQL for aggregate results?

Guys,

 

i found following solution but this one shows the result based on days in month.

 

select day_only(createdDate) createdonDate,
count(createdDate) numCreated
from account
group by day_only(createdDate)
order by day_only(createdDate) desc

 

But i want result based on month.

 

 

 

Regards,

Vishal Thoriya.

 

 

dhelmerdhelmer

Are you looking for CALENDAR_MONTH instead of DAY_ONLY?

 

Found it here under "Date Functions"

Vishal_ThoriyaVishal_Thoriya

CALENDAR_MONTH will not work for date time fields. Is there any other work around for date time fields?

davidandrewhelmerdavidandrewhelmer

I just tried it on a datetime field and it worked.


Also it's demonstrated here

 

SELECT CALENDAR_YEAR(CreatedDate), SUM(Amount)
FROM Opportunity
GROUP BY CALENDAR_YEAR(CreatedDate)
Vishal_ThoriyaVishal_Thoriya

I have tried with following code and it worked for me.

 

SELECT HOUR_IN_DAY(convertTimezone(CreatedDate)), SUM(Amount)
FROM Opportunity
GROUP BY HOUR_IN_DAY(convertTimezone(CreatedDate))