Use um
DISTINCT
subconsulta para o primeiro par de colunas e, em seguida, use um segundo DISTINCT
subconsulta para o outro par de colunas e JOIN
no test_col1
comum :SELECT JSON_OBJECT (
'output' VALUE JSON_ARRAYAGG(
JSON_OBJECT(
'common' VALUE c23.test_col1,
'list' VALUE c23.list,
'anotherlist' VALUE c56.anotherlist
)
)
)
FROM (
SELECT test_col1,
JSON_ARRAYAGG(
JSON_OBJECT(
'key1' VALUE test_col2,
'key2' VALUE test_col3
)
) AS list
FROM ( SELECT DISTINCT
test_col1, test_col2, test_col3
FROM test_tbl
WHERE test_col4 = 'val7'
)
GROUP BY test_col1
) c23
INNER JOIN (
SELECT test_col1,
JSON_ARRAYAGG(
JSON_OBJECT(
'key1' VALUE test_col5,
'key2' VALUE test_col6
)
) AS anotherlist
FROM ( SELECT DISTINCT
test_col1, test_col5, test_col6
FROM test_tbl
WHERE test_col4 = 'val7'
)
GROUP BY test_col1
) c56
ON ( c23.test_col1 = c56.test_col1 )
Saídas: