SELECT key1, key2, JSON_ARRAYAGG(foo) foo, JSON_ARRAYAGG(bar) bar FROM (
select 1 as key1, 2 as key2, '1.0' as foo, 'A' as bar from dual
UNION
select 1, 2, '2.0' , 'A' as bar from dual
UNION
select 3, 4, '2.0' , 'A' as bar from dual
UNION
select 3, 4, '2.0' , 'B' as bar from dual
UNION
select 3, 4, '2.0' , 'B' as bar from dual) z
GROUP BY key1, key2
The query returns following result:
1 2 ["1.0","2.0"] ["A","A"]
3 4 ["2.0","2.0"] ["A","B"]
I was expecting
1 2 ["1.0","2.0"] ["A"]
3 4 ["2.0"] ["A","B"]
I seems that JSON_ARRAYAGG doesn't support DISTINCT, any suggestions?
You can use
COLLECT(DISTINCT ...)to perform the aggregation and then convert the generated collection to JSON:Which, for the sample data:
Outputs:
fiddle