Hi I created a query to generate JSON but I am getting ORA-00935: group function is nested too deeply. I am using Oracle 19c.
Below is the query I wrote but my grouping is somehow repeating.When I use JSON_ARRAYAGG or JSON_OBJECTAGG it gives the above error.I am missing something .Do I have to call the table again?
WITH sub(dept,phone,device,grp) AS
(SELECT 'STAFF' ,'1234567890','google' ,1 FROM dual UNION
SELECT 'STAFF' ,'0004567890','apple' ,1 FROM dual UNION
SELECT 'CEO' ,'0094567890','google' ,1 FROM dual UNION
SELECT 'CEO' ,'0055567890','google' ,1 FROM dual UNION
SELECT 'MGR' ,'0085456789','google' ,2 FROM dual UNION
SELECT 'MGR' ,'0704545478','google' ,2 FROM dual UNION
SELECT 'MGR' ,'0654565460','apple' ,2 FROM dual UNION
SELECT 'EXEC' ,'0084467890','apple' ,2 FROM dual UNION
SELECT 'TRAINEE' ,'1234770000','apple' ,3 FROM dual UNION
SELECT 'TRAINEE' ,'0354430000','apple' ,3 FROM dual UNION
SELECT 'TRAINEE' ,'0067556557','google' ,3 FROM dual)
SELECT JSON_OBJECTAGG(KEY 'grp'||grp VALUE
(JSON_OBJECTAGG(KEY device VALUE
(JSON_ARRAY
(JSON_OBJECT( KEY 'dept' VALUE dept,
KEY 'sound' VALUE 'defeault.caf',
KEY 'token' VALUE JSON_ARRAY(phone)
)
)
)RETURNING CLOB PRETTY
)
) RETURNING CLOB PRETTY
) AS result
FROM sub GROUP BY grp;**
- Expected Output
{
"grp1":
{
"google":
[{"dept": "STAFF",
"sound": "default.caf",
"phone": ["1234567890"]
},
{"dept": "CEO",
"sound": "default.caf",
"phone": ["0094567890","0055567890"]
}
],
"apple":
[{"dept": "STAFF",
"sound": "default.caf",
"phone": ["0004567890"]
}
]
},
"grp2":
{
"google":
[{"dept": "MGR",
"sound": "default.caf",
"phone": ["0085456789","0704545478"]
}
],
"apple":
[{"dept": "MGR",
"sound": "default.caf",
"phone": ["0654565460"]
},
{"dept": "EXEC",
"sound": "default.caf",
"phone": ["0084467890"]
}
]
},
"grp3":
{
"google":
[{"dept": "TRAINEE",
"sound": "default.caf",
"phone": ["0067556557"]
}
],
"apple":
[{"dept": "TRAINEE",
"sound": "default.caf",
"phone": ["0354430000","1234770000"]
}
]
}
}