I'm trying to understand why this works:
select
array_to_json(array_agg(row_to_json(positions_table)))
from (
select
sum(start_month),
function_id,
profile_id
from positions as subquery
group by function_id, profile_id
) as positions_table
But this does not:
select
profiles.id,
(array_to_json(array_agg(row_to_json(
select
sum(start_month),
function_id,
profile_id
from positions as subquery
group by function_id, profile_id
))))
from profiles
It seems like I'm not allowed to put the select... statement inside array_to_json(array_agg(row_to_json())) and that it needs to reference a table instead.
But I'm suspicious I might be missing something.
The error is syntax error near select.
jsonb_agg()instead for your wishful result see more. Also,row_to_jsonexpectsrecordas input, subquery is not a record it is set of records, so you can't expect function that's purpose is to convert 1 single row to json to convert N rows.