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 q.activeVersion >= c.addedVersion
        and q.activeVersion < COALESCE(c.removedVersion, q.activeVersion + 1)
    LEFT JOIN requiredQueries AS r ON q.id = r.queryId
        and q.activeVersion >= r.addedVersion
        and q.activeVersion < COALESCE(r.removedVersion, q.activeVersion + 1)
    GROUP BY q.id, q.type, q.activeversion, q.latestversion, c.config;
