I have a table
---------+-----------
| type_id | A | B | A |
---------+-----------
| stock | 1 | 2 | 2 |
---------+-----------
I'm trying to run
select stock.type_id, sum(stock.stock)
from stock group by type_id;
Turning it into JSON object
I tried running
> select
> json_arrayagg(
> JSON_ARRAY(
> 'type_id', stock.type_id,
> 'stock', sum(stock.stock)
> )
> )
> from stock group by type_id ;
But it returns Error(1111): Invalid use of group function
I tried various types of concatenation, but it still returns various errors :-/
If used without json_arrayagg part, it returns multiple rows.
If used without "group by type_id", it returns a single JSON row, but the results are all wrong and it can't be used to return the sum just separate rows :-(