Order By Date Part

To order by date part, use EXTRACT() on the date field with the ORDER BY clause. The query returns the count of rows containing these same values and orders the results by count descending.

1SELECT EXTRACT(YEAR FROM CloseDate) AS CloseDate_Year, EXTRACT(MONTH FROM CloseDate) AS CloseDate_Month, COUNT(*) AS cnt
2FROM "OpportunityFiscalEMTimezoned"
3GROUP BY EXTRACT(YEAR FROM CloseDate), EXTRACT(MONTH FROM CloseDate)
4ORDER BY cnt DESC
5LIMIT 10;
CloseDate_YearCloseDate_Monthcnt
201513
201532
201542
201552
2014121
201521
201561
201581
2015101
2015111