Saya pikir date_trunc
function mungkin teman Anda (lihat dokumen postgres ). Anda akan melakukan sesuatu seperti ini, saya kira:
select
COUNT(ID) as Total,
COUNT(CASE WHEN createddate between date_trunc('month', now()) AND now() THEN AG.ID END) as ThisMonth,
COUNT(CASE WHEN createddate between date_trunc('month', now()) - interval '1 month' AND date_trunc('month', now()) - interval '1 day' THEN AG.ID END) as LastMonth,
dll...