0
{
  "timeStamp": 1593664441878,
  "timingRecords": [
    {
      "task": "extendedClean",
      "time": 31,
      "modules": [
        "main"
      ]
    },
    {
      "task": "clean",
      "time": 35,
      "modules": [
        "lint"
      ]
    },
        {
      "task": "compile",
      "time": 35,
      "modules": [
        "test"
      ]
    }
  ]
}

This is my json data in the table. I have multiple rows of similar records.

I am looking for a result as the sum of all times where task in (extendedClean, clean)

So my final expected result would look like

timestamp    | sum(time)
1593664441878| 66
1593664741878| 22
1
  • Please edit your question (by clicking on the edit link below it) and add expected output based on your sample data. Do you only need this for a single row (=json) or do you need to handle multiple rows with different JSON values? Commented Jul 6, 2020 at 10:32

1 Answer 1

1

It's a bit unclear how you need that in the context of a complete query. But given a single JSON value as shown in your question, you can do this:

select sum( (e ->> 'time')::int )
from the_table
  cross join jsonb_array_elements(the_json_column -> 'timingRecords') as e
where e ->> 'task' in ('extendedClean', 'clean');

Online example

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.