Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

Trim chat, quote copy-paste material
Source Link
halfer
  • 20.2k
  • 20
  • 111
  • 208

Please help me write a query. I have a table with data on user applications. It It contains several fields:employee_id text, date date, app_info jsonb.

I have the following task: For a given date range, calculate the number of minutes in each application with an interval of 15 minutes. That is, I need to combine the existing 5-minute granules into 15-minute granules and calculate the amount of time for all applications in this granule for each granule. Group the list of applications itself so that there are no duplicates. I tried to do this, but I can’t figure out how to write the query correctly.

and I need a second query that finds the largest granule by application runtime. That is, where the greatest amount of work is in different applications. I don’t understand how to write this query at all...

For a given date range, calculate the number of minutes in each application with an interval of 15 minutes. That is, I need to combine the existing 5-minute granules into 15-minute granules and calculate the amount of time for all applications in this granule for each granule. Group the list of applications itself so that there are no duplicates.

I tried to do this, but I can’t figure out how to write the query correctly.

And I need a second query that finds the largest granule by application runtime. That is, where the greatest amount of work is in different applications. I don’t understand how to write this query at all...

P.S. I used your answers and am trying to create a new query based on the table provided in the question. I

I need to calculate the total activity time for all applications in the granule for each 5-minute granule (these granules are already in the table). Find the application that has the largest number of seconds, and I need to display the application name and productivity rating. Sort all applications in the granule by time and productivity rating. Merge identical granules. Granules in which the most used application is the same are considered identical. After merging, get an extended granule with a count of the number of seconds. For example, we have granules:

Find the application that has the largest number of seconds, and I need to display the application name and productivity rating. Sort all applications in the granule by time and productivity rating.

Merge identical granules. Granules in which the most used application is the same are considered identical. After merging, get an extended granule with a count of the number of seconds. For example, we have granules:

Only adjacent granules need to be merged. And the granula_start and granula_end fields are changed into one granule per 10 minutes. This This merging rule applies to all granules, both lower and higher in time. I understand that I will need to use recursion here, but I can't write such a query. I

I also couldn't select not just the maximum number of seconds in the array of applications, but immediately an object that contains the maximum number of seconds with information about the application name and productivity rating. There is also a rule for sorting by productivity ratings: unproductive, productive, no rating, neutral, inaction. And you need to sort all the applications in each granule according to this order. But first we sort by time, and then by rating. As a result, I should get a query like this:

There is also a rule for sorting by productivity ratings: unproductive, productive, no rating, neutral, inaction. And you need to sort all the applications in each granule according to this order. But first we sort by time, and then by rating. As a result, I should get a query like this:

I tried to achieve this result all day, but I couldn't... I see that you have good experience in writing queries, could you help me write such a query, please?

Please, don't give me a negative rating. I really hope for your help.

Please, don't delete your previous answers - I really need them.

Please help me write a query. I have a table with data on user applications. It contains several fields:employee_id text, date date, app_info jsonb.

I have the following task: For a given date range, calculate the number of minutes in each application with an interval of 15 minutes. That is, I need to combine the existing 5-minute granules into 15-minute granules and calculate the amount of time for all applications in this granule for each granule. Group the list of applications itself so that there are no duplicates. I tried to do this, but I can’t figure out how to write the query correctly.

and I need a second query that finds the largest granule by application runtime. That is, where the greatest amount of work is in different applications. I don’t understand how to write this query at all...

P.S. I used your answers and am trying to create a new query based on the table provided in the question. I need to calculate the total activity time for all applications in the granule for each 5-minute granule (these granules are already in the table). Find the application that has the largest number of seconds, and I need to display the application name and productivity rating. Sort all applications in the granule by time and productivity rating. Merge identical granules. Granules in which the most used application is the same are considered identical. After merging, get an extended granule with a count of the number of seconds. For example, we have granules:

Only adjacent granules need to be merged. And the granula_start and granula_end fields are changed into one granule per 10 minutes. This merging rule applies to all granules, both lower and higher in time. I understand that I will need to use recursion here, but I can't write such a query. I also couldn't select not just the maximum number of seconds in the array of applications, but immediately an object that contains the maximum number of seconds with information about the application name and productivity rating. There is also a rule for sorting by productivity ratings: unproductive, productive, no rating, neutral, inaction. And you need to sort all the applications in each granule according to this order. But first we sort by time, and then by rating. As a result, I should get a query like this:

I tried to achieve this result all day, but I couldn't... I see that you have good experience in writing queries, could you help me write such a query, please?

Please, don't give me a negative rating. I really hope for your help.

Please, don't delete your previous answers - I really need them.

I have a table with data on user applications. It contains several fields:employee_id text, date date, app_info jsonb.

I have the following task:

For a given date range, calculate the number of minutes in each application with an interval of 15 minutes. That is, I need to combine the existing 5-minute granules into 15-minute granules and calculate the amount of time for all applications in this granule for each granule. Group the list of applications itself so that there are no duplicates.

I tried to do this, but I can’t figure out how to write the query correctly.

And I need a second query that finds the largest granule by application runtime. That is, where the greatest amount of work is in different applications. I don’t understand how to write this query at all...

P.S. I used your answers and am trying to create a new query based on the table provided in the question.

I need to calculate the total activity time for all applications in the granule for each 5-minute granule (these granules are already in the table).

Find the application that has the largest number of seconds, and I need to display the application name and productivity rating. Sort all applications in the granule by time and productivity rating.

Merge identical granules. Granules in which the most used application is the same are considered identical. After merging, get an extended granule with a count of the number of seconds. For example, we have granules:

Only adjacent granules need to be merged. And the granula_start and granula_end fields are changed into one granule per 10 minutes. This merging rule applies to all granules, both lower and higher in time. I understand that I will need to use recursion here, but I can't write such a query.

I also couldn't select not just the maximum number of seconds in the array of applications, but immediately an object that contains the maximum number of seconds with information about the application name and productivity rating.

There is also a rule for sorting by productivity ratings: unproductive, productive, no rating, neutral, inaction. And you need to sort all the applications in each granule according to this order. But first we sort by time, and then by rating. As a result, I should get a query like this:

deleted 162 characters in body
Source Link

Please help me write a query. I have a table with data on user applications. It contains several fields: employee_id text, date date, app_info jsonb. The

+-------------+------------+------------+
| employee_id | date | app_info |
+-------------+------------+------------+
| 2223eb0f0d0x| 01/07/2025 | jsonb |
| 2223eb0f0d0x| 01/08/2025 | jsonb |
| 2223eb0f0d0x| 31/07/2025 | jsonb |
| 2223eb0f0d0x| 31/08/2025 | jsonb |
+-------------+------------+------------+

The info field app_info contains the following code:

[
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 155.334
 }
 ],
 "granula_end": "14:50:00",
 "granula_start": "14:45:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 282.565
 },
 {
 "date""grade": "2025年07月31日""UNPRODUCTIVE",
 "app_name": "WinSCP: SFTP, FTP, WebDAV, S3 and SCP"Google client"chrome",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
  "count_seconds""http: 459//vk.091
 },
 {
 "date": "2025年07月31日",
 "app_name": "Проводник",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b"com",
 "count_seconds": 47457.966731
 }
 ],
 "granula_end": "14:55:00",
 "granula_start": "14:50:00"
 },
 {
 "apps": null,
 "granula_end": "15:00:00",
 "granula_start": "14:55:00"
 },
 {
 "apps": null,
 "granula_end": "15:05:00",
 "granula_start": "15:00:00"
 },
 {
 "apps": null,
 "granula_end": "15:10:00",
 "granula_start": "15:05:00"
 },
 {
 "apps": null,
 "granula_end": "15:15:00",
 "granula_start": "15:10:00"
 },
 {
 "apps": null,
  "granula_end": "15:20:00",
 "granula_start": "15:15:00"
 },
 {
 "apps": null,
 "granula_end": "15:25:00",
 "granula_start": "15:20:00"
 },
 {
 "apps": null,
 "granula_end": "15:30:00",
 "granula_start": "15:25:00"
 },
 {
 "apps": null,
 "granula_end": "15:35:00",
 "granula_start": "15:30:00"
 },
 {
 "apps": null,
 "granula_end": "15:40:00",
 "granula_start": "15:35:00"
 },
 {
 "apps": null,
 "granula_end": "15:45:00",
 "granula_start": "15:40:00"
 },
 {
 "apps": null,
 "granula_end": "15:50:00",
 "granula_start": "15:45:00"
 },
 {
 "apps": null,
 "granula_end": "15"17:55:00",
 "granula_start": "15"17:50:00"
 },
 {
 "apps": null,
 "granula_end": "16:00:00",
 "granula_start": "15:55:00"
 },
 {
 "apps": null,
 "granula_end": "16:05:00",
 "granula_start": "16:00:00"
 },
 {
 "apps": null,
 "granula_end": "16:10:00",
 "granula_start": "16:05:00"
 },
 {
 "apps": [
 {
 "date""grade": "2025年07月31日""UNPRODUCTIVE",
 "app_name": "CLion""Google chrome",
 "type_name": "Native",
 "domain_site": "",
  "employee_id""http: "2223eb0f0d0c4941a16e83dc7274771b"//vk.com",
 "count_seconds": 243217.496879
 },
 {
 "date""grade": "2025年07月31日""PRODUCTIVE",
 "app_name": "Postman""Windows Explorer",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
  "count_seconds": 3193.902174
 }
 ],
 "granula_end": "16:15:00",
 "granula_start": "16:10:00"
 },
 {
  "apps": null,
 "granula_end": "16:20:00",
 "granula_start": "16:15:00"
 },
 {
 "apps": null,
 "granula_end": "16"19:25:00",
 "granula_start": "16"19:20:00"
 },
 {
 "apps": null,
 "granula_end": "16:30:00",
 "granula_start": "16:25:00"
 },
 {
 "apps": null,
 "granula_end": "16:35:00",
 "granula_start": "16:30:00"
 },
 {
 "apps": null,
 "granula_end": "16:40:00",
 "granula_start": "16:35:00"
 },
 {
 "apps": null,
 "granula_end": "16:45:00",
 "granula_start": "16:40:00"
 },
  {
 "apps": null,
 "granula_end": "16:50:00",
 "granula_start": "16:45:00"
 },
 {
 "apps": null,
 "granula_end": "16:55:00",
 "granula_start": "16:50:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 192.897
 },
 {
 "date""grade": "2025年07月31日""NEUTRAL",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 111.307
 },
 {
 "date": "2025年07月31日",
 "Time "app_name":Doctor "Проводник"2",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 144118.277299
 }
 ],
 "granula_end": "17:00:00",
 "granula_start": "16:55:00"
 },
 {
 "apps": null,
 "granula_end": "17:05:00",
 "granula_start": "17:00:00"
 },
 {
 "apps": null,
 "granula_end": "17:10:00",
 "granula_start": "17:05:00"
 },
 {
 "apps": null,
 "granula_end": "17:15:00",
 "granula_start": "17:10:00"
 },
 {
 "apps": null,
 "granula_end": "17:20:00",
 "granula_start": "17:15:00"
 },
 {
 "apps": null,
 "granula_end": "17:25:00",
 "granula_start": "17:20:00"
 },
 {
 "apps": null,
 "granula_end": "17"19:30:00",
 "granula_start": "17"19:25:00"
 },
 {
 "apps": null,
 "granula_end": "17:35:00",
 "granula_start": "17:30:00"
  },
 {
 "apps": [
 {
 "date""grade": "2025年07月31日""NEUTRAL",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
  "count_seconds": 66.195
 },
 {
 "date": "2025年07月31日",
 "Time "app_name":Doctor "Postman"2",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 4529.276
 }
 ],
 "granula_end": "17:40:00",
 "granula_start": "17:35:00"
 },
 {
 "apps": null,
 "granula_end": "17:45:00",
 "granula_start": "17:40:00"
 },
 {
 "apps": null,
 "granula_end": "17:50:00",992
 "granula_start": "17:45:00"
 },
 {
 "apps": [
 {
 "date""grade": "2025年07月31日""PRODUCTIVE",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 3.86
 },
 {
 "date": "2025年07月31日",
  "app_name":"Windows "Postman"Explorer",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 93.65002
 }
 ],
 "granula_end": "17"19:55:00",
 "granula_start": "17"19:50:00"
 },
 {
 "apps": null,
 "granula_end": "18:00:00",
 "granula_start": "17:55:00"
 }
]

P.S. I used your answers and am trying to create a new query based on the table provided in the question. I need to calculate the total activity time for all applications in the granule for each 5-minute granule (these granules are already in the table). Find the application that has the largest number of seconds, and I need to display the application name and productivity rating. Sort all applications in the granule by time and productivity rating. Merge identical granules. Granules in which the most used application is the same are considered identical. After merging, get an extended granule with a count of the number of seconds. For example, we have granules:

[
 {
 "apps": [
 {
 "grade": "UNPRODUCTIVE",
 "app_name": "Google chrome",
 "type_name": "Native",
 "domain_site": "http://vk.com",
 "count_seconds": 57.731
 }
 ],
 "granula_end": "16:55:00",
 "granula_start": "16:50:00"
 },
 {
 "apps": [
 {
 "grade": "UNPRODUCTIVE",
 "app_name": "Google chrome",
 "type_name": "Native",
 "domain_site": "http://vk.com",
 "count_seconds": 217.879
 },
 {
 "grade": "PRODUCTIVE",
 "app_name": "Windows Explorer",
 "type_name": "Native",
 "domain_site": "",
 "count_seconds": 3.174
 }
 ],
 "granula_end": "16:55:00",
 "granula_start": "17:00:00"
 }]

You can see that these two granules have the same most used application. And the granules are adjacent to each other in time! These are the granules that need to be merged into one:

{
 "apps": [
 {
 "grade": "UNPRODUCTIVE",
 "app_name": "Google chrome",
 "type_name": "Native",
 "domain_site": "http://vk.com",
 "count_seconds": 275.61
 },
 {
 "grade": "PRODUCTIVE",
 "app_name": "Windows Explorer",
 "type_name": "Native",
 "domain_site": "",
 "count_seconds": 3.174
 }
 ],
 "granula_end": "16:50:00",
 "granula_start": "17:00:00"
 }

Only adjacent granules need to be merged. And the granula_start and granula_end fields are changed into one granule per 10 minutes. This merging rule applies to all granules, both lower and higher in time. I understand that I will need to use recursion here, but I can't write such a query. I also couldn't select not just the maximum number of seconds in the array of applications, but immediately an object that contains the maximum number of seconds with information about the application name and productivity rating. There is also a rule for sorting by productivity ratings: unproductive, productive, no rating, neutral, inaction. And you need to sort all the applications in each granule according to this order. But first we sort by time, and then by rating. As a result, I should get a query like this:

{
 "userInfo": {
 "employeeId": "2223eb0f0d0c4941a16e83dc7274771b",
 },
 "granules": [
 {
 "dateTimeStart": "2025年07月08日T12:30",
 "dateTimeEnd": "2025年07月08日T12:35",
 "highestTypeActivity": "PRODUCTIVE",
 "activeTime": 210,
 "apps:" [
 {
 "name": "telegram.exe",
 "activeTime": 140,
 "grade": "PRODUCTIVE"
 },
 {
 "name": "notepad.exe",
 "activeTime": 70,
 "grade": "PRODUCTIVE"
 }
 ]
 },
 {
 "dateTimeStart": "2025年07月08日T12:35",
 "dateTimeEnd": "2025年07月08日T12:40",
 "activeTime": 210,
 "apps:" [
 {
 "name": "google chrome",
 "details": "http://vk.com",
 "activeTime": 140,
 "grade": "UNPRODUCTIVE"
 },
 {
 "name": "google chrome",
 "details": "http://mail.ru",
 "activeTime": 70,
 "grade": "PRODUCTIVE"
 }
 ]
 },
 ]
 
 }

I tried to achieve this result all day, but I couldn't... I see that you have good experience in writing queries, could you help me write such a query, please?

I am update table in https://dbfiddle.uk/K5MnXSjx

Please, don't give me a negative rating. I really hope for your help.

Please, don't delete your previous answers - I really need them.

Please help me write a query. I have a table with data on user applications. It contains several fields: employee_id text, date date, app_info jsonb. The info field app_info contains the following code:

[
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 155.334
 }
 ],
 "granula_end": "14:50:00",
 "granula_start": "14:45:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 282.565
 },
 {
 "date": "2025年07月31日",
 "app_name": "WinSCP: SFTP, FTP, WebDAV, S3 and SCP client",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
  "count_seconds": 459.091
 },
 {
 "date": "2025年07月31日",
 "app_name": "Проводник",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 474.966
 }
 ],
 "granula_end": "14:55:00",
 "granula_start": "14:50:00"
 },
 {
 "apps": null,
 "granula_end": "15:00:00",
 "granula_start": "14:55:00"
 },
 {
 "apps": null,
 "granula_end": "15:05:00",
 "granula_start": "15:00:00"
 },
 {
 "apps": null,
 "granula_end": "15:10:00",
 "granula_start": "15:05:00"
 },
 {
 "apps": null,
 "granula_end": "15:15:00",
 "granula_start": "15:10:00"
 },
 {
 "apps": null,
  "granula_end": "15:20:00",
 "granula_start": "15:15:00"
 },
 {
 "apps": null,
 "granula_end": "15:25:00",
 "granula_start": "15:20:00"
 },
 {
 "apps": null,
 "granula_end": "15:30:00",
 "granula_start": "15:25:00"
 },
 {
 "apps": null,
 "granula_end": "15:35:00",
 "granula_start": "15:30:00"
 },
 {
 "apps": null,
 "granula_end": "15:40:00",
 "granula_start": "15:35:00"
 },
 {
 "apps": null,
 "granula_end": "15:45:00",
 "granula_start": "15:40:00"
 },
 {
 "apps": null,
 "granula_end": "15:50:00",
 "granula_start": "15:45:00"
 },
 {
 "apps": null,
 "granula_end": "15:55:00",
 "granula_start": "15:50:00"
 },
 {
 "apps": null,
 "granula_end": "16:00:00",
 "granula_start": "15:55:00"
 },
 {
 "apps": null,
 "granula_end": "16:05:00",
 "granula_start": "16:00:00"
 },
 {
 "apps": null,
 "granula_end": "16:10:00",
 "granula_start": "16:05:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
  "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 243.496
 },
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
  "count_seconds": 319.902
 }
 ],
 "granula_end": "16:15:00",
 "granula_start": "16:10:00"
 },
 {
  "apps": null,
 "granula_end": "16:20:00",
 "granula_start": "16:15:00"
 },
 {
 "apps": null,
 "granula_end": "16:25:00",
 "granula_start": "16:20:00"
 },
 {
 "apps": null,
 "granula_end": "16:30:00",
 "granula_start": "16:25:00"
 },
 {
 "apps": null,
 "granula_end": "16:35:00",
 "granula_start": "16:30:00"
 },
 {
 "apps": null,
 "granula_end": "16:40:00",
 "granula_start": "16:35:00"
 },
 {
 "apps": null,
 "granula_end": "16:45:00",
 "granula_start": "16:40:00"
 },
  {
 "apps": null,
 "granula_end": "16:50:00",
 "granula_start": "16:45:00"
 },
 {
 "apps": null,
 "granula_end": "16:55:00",
 "granula_start": "16:50:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 192.897
 },
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 111.307
 },
 {
 "date": "2025年07月31日",
  "app_name": "Проводник",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 144.277
 }
 ],
 "granula_end": "17:00:00",
 "granula_start": "16:55:00"
 },
 {
 "apps": null,
 "granula_end": "17:05:00",
 "granula_start": "17:00:00"
 },
 {
 "apps": null,
 "granula_end": "17:10:00",
 "granula_start": "17:05:00"
 },
 {
 "apps": null,
 "granula_end": "17:15:00",
 "granula_start": "17:10:00"
 },
 {
 "apps": null,
 "granula_end": "17:20:00",
 "granula_start": "17:15:00"
 },
 {
 "apps": null,
 "granula_end": "17:25:00",
 "granula_start": "17:20:00"
 },
 {
 "apps": null,
 "granula_end": "17:30:00",
 "granula_start": "17:25:00"
 },
 {
 "apps": null,
 "granula_end": "17:35:00",
 "granula_start": "17:30:00"
  },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
  "count_seconds": 66.195
 },
 {
 "date": "2025年07月31日",
  "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 45.276
 }
 ],
 "granula_end": "17:40:00",
 "granula_start": "17:35:00"
 },
 {
 "apps": null,
 "granula_end": "17:45:00",
 "granula_start": "17:40:00"
 },
 {
 "apps": null,
 "granula_end": "17:50:00",
 "granula_start": "17:45:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 3.86
 },
 {
 "date": "2025年07月31日",
  "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 9.65
 }
 ],
 "granula_end": "17:55:00",
 "granula_start": "17:50:00"
 },
 {
 "apps": null,
 "granula_end": "18:00:00",
 "granula_start": "17:55:00"
 }
]

Please help me write a query. I have a table with data on user applications. It contains several fields: employee_id text, date date, app_info jsonb.

+-------------+------------+------------+
| employee_id | date | app_info |
+-------------+------------+------------+
| 2223eb0f0d0x| 01/07/2025 | jsonb |
| 2223eb0f0d0x| 01/08/2025 | jsonb |
| 2223eb0f0d0x| 31/07/2025 | jsonb |
| 2223eb0f0d0x| 31/08/2025 | jsonb |
+-------------+------------+------------+

The info field app_info contains the following code:

[
 {
 "apps": [
 {
 "grade": "UNPRODUCTIVE",
 "app_name": "Google chrome",
 "type_name": "Native",
 "domain_site": "http://vk.com",
 "count_seconds": 57.731
 }
 ],
 "granula_end": "17:55:00",
 "granula_start": "17:50:00"
 },
 {
 "apps": [
 {
 "grade": "UNPRODUCTIVE",
 "app_name": "Google chrome",
 "type_name": "Native",
 "domain_site": "http://vk.com",
 "count_seconds": 217.879
 },
 {
 "grade": "PRODUCTIVE",
 "app_name": "Windows Explorer",
 "type_name": "Native",
 "domain_site": "",
 "count_seconds": 3.174
 }
 ],
 "granula_end": "19:25:00",
 "granula_start": "19:20:00"
 },
 {
 "apps": [
 {
 "grade": "NEUTRAL",
 "app_name": "Time Doctor 2",
 "type_name": "Native",
 "domain_site": "",
 "count_seconds": 118.299
 }
 ],
 "granula_end": "19:30:00",
 "granula_start": "19:25:00"
 },
 {
 "apps": [
 {
 "grade": "NEUTRAL",
 "app_name": "Time Doctor 2",
 "type_name": "Native",
 "domain_site": "",
 "count_seconds": 29.992
 },
 {
 "grade": "PRODUCTIVE",
 "app_name": "Windows Explorer",
 "type_name": "Native",
 "domain_site": "",
 "count_seconds": 3.002
 }
 ],
 "granula_end": "19:55:00",
 "granula_start": "19:50:00"
 }
]

P.S. I used your answers and am trying to create a new query based on the table provided in the question. I need to calculate the total activity time for all applications in the granule for each 5-minute granule (these granules are already in the table). Find the application that has the largest number of seconds, and I need to display the application name and productivity rating. Sort all applications in the granule by time and productivity rating. Merge identical granules. Granules in which the most used application is the same are considered identical. After merging, get an extended granule with a count of the number of seconds. For example, we have granules:

[
 {
 "apps": [
 {
 "grade": "UNPRODUCTIVE",
 "app_name": "Google chrome",
 "type_name": "Native",
 "domain_site": "http://vk.com",
 "count_seconds": 57.731
 }
 ],
 "granula_end": "16:55:00",
 "granula_start": "16:50:00"
 },
 {
 "apps": [
 {
 "grade": "UNPRODUCTIVE",
 "app_name": "Google chrome",
 "type_name": "Native",
 "domain_site": "http://vk.com",
 "count_seconds": 217.879
 },
 {
 "grade": "PRODUCTIVE",
 "app_name": "Windows Explorer",
 "type_name": "Native",
 "domain_site": "",
 "count_seconds": 3.174
 }
 ],
 "granula_end": "16:55:00",
 "granula_start": "17:00:00"
 }]

You can see that these two granules have the same most used application. And the granules are adjacent to each other in time! These are the granules that need to be merged into one:

{
 "apps": [
 {
 "grade": "UNPRODUCTIVE",
 "app_name": "Google chrome",
 "type_name": "Native",
 "domain_site": "http://vk.com",
 "count_seconds": 275.61
 },
 {
 "grade": "PRODUCTIVE",
 "app_name": "Windows Explorer",
 "type_name": "Native",
 "domain_site": "",
 "count_seconds": 3.174
 }
 ],
 "granula_end": "16:50:00",
 "granula_start": "17:00:00"
 }

Only adjacent granules need to be merged. And the granula_start and granula_end fields are changed into one granule per 10 minutes. This merging rule applies to all granules, both lower and higher in time. I understand that I will need to use recursion here, but I can't write such a query. I also couldn't select not just the maximum number of seconds in the array of applications, but immediately an object that contains the maximum number of seconds with information about the application name and productivity rating. There is also a rule for sorting by productivity ratings: unproductive, productive, no rating, neutral, inaction. And you need to sort all the applications in each granule according to this order. But first we sort by time, and then by rating. As a result, I should get a query like this:

{
 "userInfo": {
 "employeeId": "2223eb0f0d0c4941a16e83dc7274771b",
 },
 "granules": [
 {
 "dateTimeStart": "2025年07月08日T12:30",
 "dateTimeEnd": "2025年07月08日T12:35",
 "highestTypeActivity": "PRODUCTIVE",
 "activeTime": 210,
 "apps:" [
 {
 "name": "telegram.exe",
 "activeTime": 140,
 "grade": "PRODUCTIVE"
 },
 {
 "name": "notepad.exe",
 "activeTime": 70,
 "grade": "PRODUCTIVE"
 }
 ]
 },
 {
 "dateTimeStart": "2025年07月08日T12:35",
 "dateTimeEnd": "2025年07月08日T12:40",
 "activeTime": 210,
 "apps:" [
 {
 "name": "google chrome",
 "details": "http://vk.com",
 "activeTime": 140,
 "grade": "UNPRODUCTIVE"
 },
 {
 "name": "google chrome",
 "details": "http://mail.ru",
 "activeTime": 70,
 "grade": "PRODUCTIVE"
 }
 ]
 },
 ]
 
 }

I tried to achieve this result all day, but I couldn't... I see that you have good experience in writing queries, could you help me write such a query, please?

I am update table in https://dbfiddle.uk/K5MnXSjx

Please, don't give me a negative rating. I really hope for your help.

Please, don't delete your previous answers - I really need them.

added 14 characters in body
Source Link
Marc Le Bihan
  • 3.6k
  • 6
  • 35
  • 74

Please help me write a query. I have a table with data on user applications. It contains several fields: employee_id textemployee_id text, date datedate date, app_info jsonbapp_info jsonb. The info field app_infofield app_info contains the following code:

[
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 155.334
 }
 ],
 "granula_end": "14:50:00",
 "granula_start": "14:45:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 282.565
 },
 {
 "date": "2025年07月31日",
 "app_name": "WinSCP: SFTP, FTP, WebDAV, S3 and SCP client",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 459.091
 },
 {
 "date": "2025年07月31日",
 "app_name": "Проводник",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 474.966
 }
 ],
 "granula_end": "14:55:00",
 "granula_start": "14:50:00"
 },
 {
 "apps": null,
 "granula_end": "15:00:00",
 "granula_start": "14:55:00"
 },
 {
 "apps": null,
 "granula_end": "15:05:00",
 "granula_start": "15:00:00"
 },
 {
 "apps": null,
 "granula_end": "15:10:00",
 "granula_start": "15:05:00"
 },
 {
 "apps": null,
 "granula_end": "15:15:00",
 "granula_start": "15:10:00"
 },
 {
 "apps": null,
 "granula_end": "15:20:00",
 "granula_start": "15:15:00"
 },
 {
 "apps": null,
 "granula_end": "15:25:00",
 "granula_start": "15:20:00"
 },
 {
 "apps": null,
 "granula_end": "15:30:00",
 "granula_start": "15:25:00"
 },
 {
 "apps": null,
 "granula_end": "15:35:00",
 "granula_start": "15:30:00"
 },
 {
 "apps": null,
 "granula_end": "15:40:00",
 "granula_start": "15:35:00"
 },
 {
 "apps": null,
 "granula_end": "15:45:00",
 "granula_start": "15:40:00"
 },
 {
 "apps": null,
 "granula_end": "15:50:00",
 "granula_start": "15:45:00"
 },
 {
 "apps": null,
 "granula_end": "15:55:00",
 "granula_start": "15:50:00"
 },
 {
 "apps": null,
 "granula_end": "16:00:00",
 "granula_start": "15:55:00"
 },
 {
 "apps": null,
 "granula_end": "16:05:00",
 "granula_start": "16:00:00"
 },
 {
 "apps": null,
 "granula_end": "16:10:00",
 "granula_start": "16:05:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 243.496
 },
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 319.902
 }
 ],
 "granula_end": "16:15:00",
 "granula_start": "16:10:00"
 },
 {
 "apps": null,
 "granula_end": "16:20:00",
 "granula_start": "16:15:00"
 },
 {
 "apps": null,
 "granula_end": "16:25:00",
 "granula_start": "16:20:00"
 },
 {
 "apps": null,
 "granula_end": "16:30:00",
 "granula_start": "16:25:00"
 },
 {
 "apps": null,
 "granula_end": "16:35:00",
 "granula_start": "16:30:00"
 },
 {
 "apps": null,
 "granula_end": "16:40:00",
 "granula_start": "16:35:00"
 },
 {
 "apps": null,
 "granula_end": "16:45:00",
 "granula_start": "16:40:00"
 },
 {
 "apps": null,
 "granula_end": "16:50:00",
 "granula_start": "16:45:00"
 },
 {
 "apps": null,
 "granula_end": "16:55:00",
 "granula_start": "16:50:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 192.897
 },
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 111.307
 },
 {
 "date": "2025年07月31日",
 "app_name": "Проводник",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 144.277
 }
 ],
 "granula_end": "17:00:00",
 "granula_start": "16:55:00"
 },
 {
 "apps": null,
 "granula_end": "17:05:00",
 "granula_start": "17:00:00"
 },
 {
 "apps": null,
 "granula_end": "17:10:00",
 "granula_start": "17:05:00"
 },
 {
 "apps": null,
 "granula_end": "17:15:00",
 "granula_start": "17:10:00"
 },
 {
 "apps": null,
 "granula_end": "17:20:00",
 "granula_start": "17:15:00"
 },
 {
 "apps": null,
 "granula_end": "17:25:00",
 "granula_start": "17:20:00"
 },
 {
 "apps": null,
 "granula_end": "17:30:00",
 "granula_start": "17:25:00"
 },
 {
 "apps": null,
 "granula_end": "17:35:00",
 "granula_start": "17:30:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 66.195
 },
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 45.276
 }
 ],
 "granula_end": "17:40:00",
 "granula_start": "17:35:00"
 },
 {
 "apps": null,
 "granula_end": "17:45:00",
 "granula_start": "17:40:00"
 },
 {
 "apps": null,
 "granula_end": "17:50:00",
 "granula_start": "17:45:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 3.86
 },
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 9.65
 }
 ],
 "granula_end": "17:55:00",
 "granula_start": "17:50:00"
 },
 {
 "apps": null,
 "granula_end": "18:00:00",
 "granula_start": "17:55:00"
 }
]
[
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 155.334
 }
 ],
 "granula_end": "14:50:00",
 "granula_start": "14:45:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 282.565
 },
 {
 "date": "2025年07月31日",
 "app_name": "WinSCP: SFTP, FTP, WebDAV, S3 and SCP client",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 459.091
 },
 {
 "date": "2025年07月31日",
 "app_name": "Проводник",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 474.966
 }
 ],
 "granula_end": "14:55:00",
 "granula_start": "14:50:00"
 },
 {
 "apps": null,
 "granula_end": "15:00:00",
 "granula_start": "14:55:00"
 },
 {
 "apps": null,
 "granula_end": "15:05:00",
 "granula_start": "15:00:00"
 },
 {
 "apps": null,
 "granula_end": "15:10:00",
 "granula_start": "15:05:00"
 },
 {
 "apps": null,
 "granula_end": "15:15:00",
 "granula_start": "15:10:00"
 },
 {
 "apps": null,
 "granula_end": "15:20:00",
 "granula_start": "15:15:00"
 },
 {
 "apps": null,
 "granula_end": "15:25:00",
 "granula_start": "15:20:00"
 },
 {
 "apps": null,
 "granula_end": "15:30:00",
 "granula_start": "15:25:00"
 },
 {
 "apps": null,
 "granula_end": "15:35:00",
 "granula_start": "15:30:00"
 },
 {
 "apps": null,
 "granula_end": "15:40:00",
 "granula_start": "15:35:00"
 },
 {
 "apps": null,
 "granula_end": "15:45:00",
 "granula_start": "15:40:00"
 },
 {
 "apps": null,
 "granula_end": "15:50:00",
 "granula_start": "15:45:00"
 },
 {
 "apps": null,
 "granula_end": "15:55:00",
 "granula_start": "15:50:00"
 },
 {
 "apps": null,
 "granula_end": "16:00:00",
 "granula_start": "15:55:00"
 },
 {
 "apps": null,
 "granula_end": "16:05:00",
 "granula_start": "16:00:00"
 },
 {
 "apps": null,
 "granula_end": "16:10:00",
 "granula_start": "16:05:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 243.496
 },
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 319.902
 }
 ],
 "granula_end": "16:15:00",
 "granula_start": "16:10:00"
 },
 {
 "apps": null,
 "granula_end": "16:20:00",
 "granula_start": "16:15:00"
 },
 {
 "apps": null,
 "granula_end": "16:25:00",
 "granula_start": "16:20:00"
 },
 {
 "apps": null,
 "granula_end": "16:30:00",
 "granula_start": "16:25:00"
 },
 {
 "apps": null,
 "granula_end": "16:35:00",
 "granula_start": "16:30:00"
 },
 {
 "apps": null,
 "granula_end": "16:40:00",
 "granula_start": "16:35:00"
 },
 {
 "apps": null,
 "granula_end": "16:45:00",
 "granula_start": "16:40:00"
 },
 {
 "apps": null,
 "granula_end": "16:50:00",
 "granula_start": "16:45:00"
 },
 {
 "apps": null,
 "granula_end": "16:55:00",
 "granula_start": "16:50:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 192.897
 },
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 111.307
 },
 {
 "date": "2025年07月31日",
 "app_name": "Проводник",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 144.277
 }
 ],
 "granula_end": "17:00:00",
 "granula_start": "16:55:00"
 },
 {
 "apps": null,
 "granula_end": "17:05:00",
 "granula_start": "17:00:00"
 },
 {
 "apps": null,
 "granula_end": "17:10:00",
 "granula_start": "17:05:00"
 },
 {
 "apps": null,
 "granula_end": "17:15:00",
 "granula_start": "17:10:00"
 },
 {
 "apps": null,
 "granula_end": "17:20:00",
 "granula_start": "17:15:00"
 },
 {
 "apps": null,
 "granula_end": "17:25:00",
 "granula_start": "17:20:00"
 },
 {
 "apps": null,
 "granula_end": "17:30:00",
 "granula_start": "17:25:00"
 },
 {
 "apps": null,
 "granula_end": "17:35:00",
 "granula_start": "17:30:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 66.195
 },
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 45.276
 }
 ],
 "granula_end": "17:40:00",
 "granula_start": "17:35:00"
 },
 {
 "apps": null,
 "granula_end": "17:45:00",
 "granula_start": "17:40:00"
 },
 {
 "apps": null,
 "granula_end": "17:50:00",
 "granula_start": "17:45:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 3.86
 },
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 9.65
 }
 ],
 "granula_end": "17:55:00",
 "granula_start": "17:50:00"
 },
 {
 "apps": null,
 "granula_end": "18:00:00",
 "granula_start": "17:55:00"
 }
]

Please help me write a query. I have a table with data on user applications. It contains several fields: employee_id text, date date, app_info jsonb. The info field app_info contains the following code:

[
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 155.334
 }
 ],
 "granula_end": "14:50:00",
 "granula_start": "14:45:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 282.565
 },
 {
 "date": "2025年07月31日",
 "app_name": "WinSCP: SFTP, FTP, WebDAV, S3 and SCP client",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 459.091
 },
 {
 "date": "2025年07月31日",
 "app_name": "Проводник",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 474.966
 }
 ],
 "granula_end": "14:55:00",
 "granula_start": "14:50:00"
 },
 {
 "apps": null,
 "granula_end": "15:00:00",
 "granula_start": "14:55:00"
 },
 {
 "apps": null,
 "granula_end": "15:05:00",
 "granula_start": "15:00:00"
 },
 {
 "apps": null,
 "granula_end": "15:10:00",
 "granula_start": "15:05:00"
 },
 {
 "apps": null,
 "granula_end": "15:15:00",
 "granula_start": "15:10:00"
 },
 {
 "apps": null,
 "granula_end": "15:20:00",
 "granula_start": "15:15:00"
 },
 {
 "apps": null,
 "granula_end": "15:25:00",
 "granula_start": "15:20:00"
 },
 {
 "apps": null,
 "granula_end": "15:30:00",
 "granula_start": "15:25:00"
 },
 {
 "apps": null,
 "granula_end": "15:35:00",
 "granula_start": "15:30:00"
 },
 {
 "apps": null,
 "granula_end": "15:40:00",
 "granula_start": "15:35:00"
 },
 {
 "apps": null,
 "granula_end": "15:45:00",
 "granula_start": "15:40:00"
 },
 {
 "apps": null,
 "granula_end": "15:50:00",
 "granula_start": "15:45:00"
 },
 {
 "apps": null,
 "granula_end": "15:55:00",
 "granula_start": "15:50:00"
 },
 {
 "apps": null,
 "granula_end": "16:00:00",
 "granula_start": "15:55:00"
 },
 {
 "apps": null,
 "granula_end": "16:05:00",
 "granula_start": "16:00:00"
 },
 {
 "apps": null,
 "granula_end": "16:10:00",
 "granula_start": "16:05:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 243.496
 },
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 319.902
 }
 ],
 "granula_end": "16:15:00",
 "granula_start": "16:10:00"
 },
 {
 "apps": null,
 "granula_end": "16:20:00",
 "granula_start": "16:15:00"
 },
 {
 "apps": null,
 "granula_end": "16:25:00",
 "granula_start": "16:20:00"
 },
 {
 "apps": null,
 "granula_end": "16:30:00",
 "granula_start": "16:25:00"
 },
 {
 "apps": null,
 "granula_end": "16:35:00",
 "granula_start": "16:30:00"
 },
 {
 "apps": null,
 "granula_end": "16:40:00",
 "granula_start": "16:35:00"
 },
 {
 "apps": null,
 "granula_end": "16:45:00",
 "granula_start": "16:40:00"
 },
 {
 "apps": null,
 "granula_end": "16:50:00",
 "granula_start": "16:45:00"
 },
 {
 "apps": null,
 "granula_end": "16:55:00",
 "granula_start": "16:50:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 192.897
 },
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 111.307
 },
 {
 "date": "2025年07月31日",
 "app_name": "Проводник",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 144.277
 }
 ],
 "granula_end": "17:00:00",
 "granula_start": "16:55:00"
 },
 {
 "apps": null,
 "granula_end": "17:05:00",
 "granula_start": "17:00:00"
 },
 {
 "apps": null,
 "granula_end": "17:10:00",
 "granula_start": "17:05:00"
 },
 {
 "apps": null,
 "granula_end": "17:15:00",
 "granula_start": "17:10:00"
 },
 {
 "apps": null,
 "granula_end": "17:20:00",
 "granula_start": "17:15:00"
 },
 {
 "apps": null,
 "granula_end": "17:25:00",
 "granula_start": "17:20:00"
 },
 {
 "apps": null,
 "granula_end": "17:30:00",
 "granula_start": "17:25:00"
 },
 {
 "apps": null,
 "granula_end": "17:35:00",
 "granula_start": "17:30:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 66.195
 },
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 45.276
 }
 ],
 "granula_end": "17:40:00",
 "granula_start": "17:35:00"
 },
 {
 "apps": null,
 "granula_end": "17:45:00",
 "granula_start": "17:40:00"
 },
 {
 "apps": null,
 "granula_end": "17:50:00",
 "granula_start": "17:45:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 3.86
 },
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 9.65
 }
 ],
 "granula_end": "17:55:00",
 "granula_start": "17:50:00"
 },
 {
 "apps": null,
 "granula_end": "18:00:00",
 "granula_start": "17:55:00"
 }
]

Please help me write a query. I have a table with data on user applications. It contains several fields: employee_id text, date date, app_info jsonb. The info field app_info contains the following code:

[
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 155.334
 }
 ],
 "granula_end": "14:50:00",
 "granula_start": "14:45:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 282.565
 },
 {
 "date": "2025年07月31日",
 "app_name": "WinSCP: SFTP, FTP, WebDAV, S3 and SCP client",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 459.091
 },
 {
 "date": "2025年07月31日",
 "app_name": "Проводник",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 474.966
 }
 ],
 "granula_end": "14:55:00",
 "granula_start": "14:50:00"
 },
 {
 "apps": null,
 "granula_end": "15:00:00",
 "granula_start": "14:55:00"
 },
 {
 "apps": null,
 "granula_end": "15:05:00",
 "granula_start": "15:00:00"
 },
 {
 "apps": null,
 "granula_end": "15:10:00",
 "granula_start": "15:05:00"
 },
 {
 "apps": null,
 "granula_end": "15:15:00",
 "granula_start": "15:10:00"
 },
 {
 "apps": null,
 "granula_end": "15:20:00",
 "granula_start": "15:15:00"
 },
 {
 "apps": null,
 "granula_end": "15:25:00",
 "granula_start": "15:20:00"
 },
 {
 "apps": null,
 "granula_end": "15:30:00",
 "granula_start": "15:25:00"
 },
 {
 "apps": null,
 "granula_end": "15:35:00",
 "granula_start": "15:30:00"
 },
 {
 "apps": null,
 "granula_end": "15:40:00",
 "granula_start": "15:35:00"
 },
 {
 "apps": null,
 "granula_end": "15:45:00",
 "granula_start": "15:40:00"
 },
 {
 "apps": null,
 "granula_end": "15:50:00",
 "granula_start": "15:45:00"
 },
 {
 "apps": null,
 "granula_end": "15:55:00",
 "granula_start": "15:50:00"
 },
 {
 "apps": null,
 "granula_end": "16:00:00",
 "granula_start": "15:55:00"
 },
 {
 "apps": null,
 "granula_end": "16:05:00",
 "granula_start": "16:00:00"
 },
 {
 "apps": null,
 "granula_end": "16:10:00",
 "granula_start": "16:05:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 243.496
 },
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 319.902
 }
 ],
 "granula_end": "16:15:00",
 "granula_start": "16:10:00"
 },
 {
 "apps": null,
 "granula_end": "16:20:00",
 "granula_start": "16:15:00"
 },
 {
 "apps": null,
 "granula_end": "16:25:00",
 "granula_start": "16:20:00"
 },
 {
 "apps": null,
 "granula_end": "16:30:00",
 "granula_start": "16:25:00"
 },
 {
 "apps": null,
 "granula_end": "16:35:00",
 "granula_start": "16:30:00"
 },
 {
 "apps": null,
 "granula_end": "16:40:00",
 "granula_start": "16:35:00"
 },
 {
 "apps": null,
 "granula_end": "16:45:00",
 "granula_start": "16:40:00"
 },
 {
 "apps": null,
 "granula_end": "16:50:00",
 "granula_start": "16:45:00"
 },
 {
 "apps": null,
 "granula_end": "16:55:00",
 "granula_start": "16:50:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 192.897
 },
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 111.307
 },
 {
 "date": "2025年07月31日",
 "app_name": "Проводник",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 144.277
 }
 ],
 "granula_end": "17:00:00",
 "granula_start": "16:55:00"
 },
 {
 "apps": null,
 "granula_end": "17:05:00",
 "granula_start": "17:00:00"
 },
 {
 "apps": null,
 "granula_end": "17:10:00",
 "granula_start": "17:05:00"
 },
 {
 "apps": null,
 "granula_end": "17:15:00",
 "granula_start": "17:10:00"
 },
 {
 "apps": null,
 "granula_end": "17:20:00",
 "granula_start": "17:15:00"
 },
 {
 "apps": null,
 "granula_end": "17:25:00",
 "granula_start": "17:20:00"
 },
 {
 "apps": null,
 "granula_end": "17:30:00",
 "granula_start": "17:25:00"
 },
 {
 "apps": null,
 "granula_end": "17:35:00",
 "granula_start": "17:30:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 66.195
 },
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 45.276
 }
 ],
 "granula_end": "17:40:00",
 "granula_start": "17:35:00"
 },
 {
 "apps": null,
 "granula_end": "17:45:00",
 "granula_start": "17:40:00"
 },
 {
 "apps": null,
 "granula_end": "17:50:00",
 "granula_start": "17:45:00"
 },
 {
 "apps": [
 {
 "date": "2025年07月31日",
 "app_name": "CLion",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 3.86
 },
 {
 "date": "2025年07月31日",
 "app_name": "Postman",
 "type_name": "Native",
 "domain_site": "",
 "employee_id": "2223eb0f0d0c4941a16e83dc7274771b",
 "count_seconds": 9.65
 }
 ],
 "granula_end": "17:55:00",
 "granula_start": "17:50:00"
 },
 {
 "apps": null,
 "granula_end": "18:00:00",
 "granula_start": "17:55:00"
 }
]
Put back the second part of the question that was lost in rev 2's deduplication
Source Link
Guillaume Outters
  • 7.6k
  • 1
  • 22
  • 28
Loading
Loading
Source Link
Loading
lang-sql

AltStyle によって変換されたページ (->オリジナル) /