SELECT
DISTINCT ON (instances.id) instances.id,
instances."duplicatedFromId",
instances."createdAt",
SUM(cr.amount)::float8 / -100 AS "Quote Amount ($)",
SUM(te.epoch_duration) / 3600 AS "timeTrackedAsHours",
profiles.name AS "DM"
FROM "instances"
INNER JOIN quotes
ON "quotes"."instanceId" = instances.id
LEFT JOIN (
SELECT
"credits"."quoteId",
"credits"."companyId",
SUM("credits".amount) AS amount
FROM "credits"
WHERE "credits"."deletedAt" IS NULL AND credits.amount < 0
GROUP BY "credits"."quoteId", "credits"."companyId"
) cr
ON cr."quoteId" = quotes.id
INNER JOIN companies
ON cr."companyId" = companies.id
LEFT JOIN "_usersToClientsAccess"
ON "_usersToClientsAccess"."clientId" = "instances"."clientId"
LEFT JOIN "users"
ON "users".id = "_usersToClientsAccess"."userId"
LEFT JOIN profiles
ON profiles.id = users."profileId"
LEFT JOIN (
SELECT
"timeEntries"."instanceId",
SUM(EXTRACT(epoch FROM "timeEntries".duration)) epoch_duration
FROM "timeEntries"
GROUP BY "timeEntries"."instanceId"
) te
ON te."instanceId" = instances.id
WHERE
"duplicatedFromId" IN (
SELECT "duplicatedFromId" FROM instances
GROUP BY "duplicatedFromId"
HAVING COUNT(*) >= 2 AND "duplicatedFromId" IS NOT NULL
) AND
"quotes"."deletedAt" IS NULL AND
instances."createdAt" > '2020-01-01 00:00:00.000000+00' AND
"_usersToClientsAccess"."roleId" = 2 AND
"companies"."externalInternal" = 'External'
GROUP BY "instances"."id", profiles.name
ORDER BY "instances"."id" DESC, "instances"."duplicatedFromId" DESC;
This query can be adapted to find only the instances where there were time tracked on it. Basically it is just add in the WHERE clause where timeTrackedAsHours is higher than 0.