Gunakan satu DISTINCT
sub-kueri untuk pasangan kolom pertama dan kemudian gunakan DISTINCT
kedua sub-kueri untuk pasangan kolom lainnya dan JOIN
pada test_col1
umum :
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 )
Keluaran: