0

In the following example I have two objects on input. I want to select the one where duration between start and end is more that one day -> I expect object called threeDays to be returned.

How to write such condition in jsonpath?

start and end are date times.

I am using Postres 13.

SELECT jsonb_path_query_array_tz('[
  { "name" : "oneDay", 
    "start": "2021-07-02 00:00:00",                                     
    "end":   "2021-07-03 00:00:00"
  },
  { "name" : "threeDays",
    "start": "2021-07-15 00:00:00",                                     
    "ende":  "2021-07-18 00:00:00"
  }
]'::jsonb, '$[*] ? (
    @.end.datetime() - @.start.datetime() > 1day     <-----  ???
).name');

Michal Foksa
  • 11,225
  • 9
  • 50
  • 68

1 Answers1

1

Here it is using jsonb_array_elements and ->> operator but w/o jsonpath.

select j
from jsonb_array_elements(
'[
  { "name" : "oneDay", 
    "start": "2021-07-02 00:00:00",                                     
    "end":   "2021-07-03 00:00:00"
  },
  { "name" : "threeDays",
    "start": "2021-07-15 00:00:00",                                     
    "end":  "2021-07-18 00:00:00"
  }
]'::jsonb) j
where (j ->> 'end')::timestamp - (j ->> 'start')::timestamp > interval '1 day';
Stefanov.sm
  • 11,215
  • 2
  • 21
  • 21