Query

WITH 

learners_groups_listed AS (
    SELECT
        groups.id,
        groups.name,
        learners.value::varchar AS learner_id
    FROM qwnogpy_ww21770_datasharing.core.groups groups,
    TABLE(FLATTEN(groups.users)) learners
),

learners_groups_aggregated AS (
    SELECT 
        learner_id,
        ARRAY_AGG(id) AS group_ids,
        ARRAY_AGG(name) AS group_names
    FROM learners_groups_listed
    GROUP BY 1
)

SELECT 
    trackings.user AS learner_id, 
    users.mail AS learner_mail,
    trackings.path_session AS path_session_id, 
    sessions.name AS path_session_name, 
    paths.name AS path_name,
    trackings.enrolled_at,
    trackings.enrolled AS is_enrolled,
    trackings.start_date, 
    trackings.completed_at, 
    trackings.progress,
    learners_groups_aggregated.group_ids AS learner_group_ids,
    learners_groups_aggregated.group_names AS learner_group_names
    
FROM qwnogpy_ww21770_datasharing.core.PATHS_TRACKINGS trackings
LEFT JOIN qwnogpy_ww21770_datasharing.core.USERS users 
    ON trackings.user = users.id
LEFT JOIN qwnogpy_ww21770_datasharing.core.PATHS_SESSIONS sessions
    ON trackings.path_session = sessions.id
LEFT JOIN qwnogpy_ww21770_datasharing.core.PATHS paths
    ON trackings.path_id = paths.id
LEFT JOIN learners_groups_aggregated
    ON trackings.user = learners_groups_aggregated.learner_id
WHERE paths.name ='' -- Replace by the path name you need, or remove the filter
    AND sessions.name='' -- Replace by the session name you need, or remove the filter
    AND ARRAY_CONTAINS(''::variant, learner_group_names) -- Replace by the group name you need, or remove the filter
ORDER BY trackings.created_at desc;

Description