PostgreSQL
 sql >> Base de Dados >  >> RDS >> PostgreSQL

Tsrange - Calculando a diferença entre dois intervalos


Você terá que separar o intervalo, dos dos documentos

Para fazer isso, você pode usar lower , e upper
SELECT tsrange(  lower(freetime), lower(appointment)  )  AS before_appointment,
       tsrange(  upper(appointment), upper(freetime)  )  AS after_appointment
FROM ( VALUES
  (
    '[2017-04-19 09:00, 2017-04-19 12:30)'::tsrange,
    '[2017-04-19 10:30, 2017-04-19 11:30)'::tsrange
  )
) AS t(freetime,appointment)
WHERE freetime @> appointment;

              before_appointment               |               after_appointment               
-----------------------------------------------+-----------------------------------------------
 ["2017-04-19 09:00:00","2017-04-19 10:30:00") | ["2017-04-19 11:30:00","2017-04-19 12:30:00")
(1 row)