CREATE VIEW fullActiveQueries AS
    SELECT DISTINCT q.id, q.type, q.activeVersion, q.latestVersion, coalesce(c.config, null) as config, ARRAY_AGG(DISTINCT r.requiredQueryId)::uuid[] as requiredIds
    FROM queries AS q
    LEFT JOIN queryConfigs AS c ON q.id = c.queryId
        and c.addedVersion >= q.activeVersion
        and COALESCE(c.removedVersion, q.activeVersion - 1) < q.activeVersion
    LEFT JOIN requiredQueries AS r ON q.id = r.queryId
        and r.addedVersion >= q.activeVersion
        and COALESCE(r.removedVersion, q.activeVersion - 1) < q.activeVersion
    GROUP BY q.id, q.type, q.activeversion, q.latestversion, c.config;
