Tengo dos mesas free_time y appointment . Ambos contienen tsranges.
¿Cómo escribo una consulta (o función) que determine el tiempo libre real después de "restar" la diferencia entre la appointment y el freetime ?
INSERT INTO free_time(freetime) VALUES('[2017-04-19 09:00, 2017-04-19 12:30)'); INSERT INTO appointment(appointment) VALUES('[2017-04-19 10:30, 2017-04-19 11:30)');Quiero que el resultado sea algo como:
["2017-04-19 9:00","2017-04-19 10:30:00"), ["2017-04-19 11:30:00","2017-04-19 12:30:00")Tendrás que separar el rango, de los documentos
Los operadores de unión y diferencia fallarán si el rango resultante necesita contener dos sub-rangos disjuntos, ya que dicho rango no se puede representar.
Para hacer esto, puede usar lower y 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)