Please help me with a request. I have two tables. The first table stores data on the user's use of the application for a certain period of time.
And I have a table of granules, which I compiled using a query on the first table.
Screen my tables:


Example value for field app_info:

But I need to get the resulting query, which for each granule will create an array of json for the programs used during the granule of time. I also need to group the programs by name or domain_site field and create an array of headers. For example, a user used the browser for 10 minutes, opening different tabs. We then need to get the following json:
[
{
"app_name": "googlechrome",
"path": "C:\\ProgramFiles\\google\\googlechrome.exe",
"domain_site": "stackoverfloww.com",
"count_seconds": 540,
"titles": [
{
"count_seconds": 320,
"url": "https://stackoverflow.com/questions/40978290/construct-json-object-from-query-with-group-by-sum",
"title": "Construct json object from query with group by / sum"
},
{
"count_seconds": 220,
"url": "https://stackoverflow.com/questions/43117033/aggregate-function-calls-cannot-be-nested-postgresql",
"title": "aggregate function calls cannot be nested postgresql"
}
]
}
]
And if the user used several applications in 10 minutes, then for each application such statistics. For each application, you need to count the number of seconds of its use.
I expect to get such a table:

I can't cope with such a request. Here is the request I wrote:
select
employee_id,
date,
granula_start,
granula_end,
(select
array_agg(json_build_object('seconds', SUM( case
when end_time > granula_end
then ((EXTRACT(MINUTE FROM granula_end) - EXTRACT(MINUTE FROM start_time))*60)
else (case when EXTRACT(MINUTE FROM start_time) = EXTRACT(MINUTE FROM end_time)
then
EXTRACT(SECOND FROM end_time) - EXTRACT(SECOND FROM start_time)
else
(EXTRACT(MINUTE FROM end_time) - EXTRACT(MINUTE FROM start_time)) * 60
end )
end ),
'domain_site', m.app_info::jsonb->'domain_site',
'app_name', m.app_info::jsonb->'app_name',
'app_type', m.app_info::jsonb->'app_type' )) as app_info
from pps.my_temp m
where start_time >= granula_start and start_time <= granula_end
group by m.app_info::jsonb->'domain_site', m.app_info::jsonb->'app_name'
)
from granules

…of your screen capture): please fix it and complete it, click the "run" button, then post the resulting URL in your question.app_infowill be more helpful than screen captures), then make the effort to add|s to make a markdown table (there even exists online services to that, for example tabletomarkdown.com/convert-spreadsheet-to-markdown). Then drop your "Example value for field app_info"; and paste the error as text.