JSON_COMPOSEを使用して、テーブルから選択した値からJSONオブジェクトを作成します。
SELECT JSON_Compose(T.company, T.employees)
FROM
(
SELECT company, JSON_agg(empID AS id,
empName AS name,
empAge AS age) AS employees
FROM emp_table
GROUP BY company
) AS T;
結果:
JSON_Compose
------------
{
"company" : "Teradata",
"employees" : [
{ "id" : 1, "name" : "Cameron", "age" : 24 },
{ "id" : 2, "name" : "Justin", "age" : 34 }
]
}
{
"company" : "Apple",
"employees" : [
{ "id" : 3, "name" : "Someone", "age" : 24 }
]
}