SET TIMEZONE='America/Los_Angeles';
SELECT
DATE_TRUNC('month', ar."accruedAt") AS month,
SUM(amount)/100.0 AS amount
FROM
"accruedRevenues" ar
JOIN instances i ON ar."instanceId" = i.id
JOIN "assistants" a ON "i"."assistantId" = a.id
JOIN "companies" c ON "a"."companyId" = c.id
WHERE
ar."deletedAt" IS NULL
AND (c."externalInternal" = 'External' OR c."externalInternal" IS NULL)
GROUP BY MONTH
ORDER BY month;