I have a tsrange like
'[2010-01-01 14:00, 2010-01-01 15:00]',20
And want to update the 20 value to 15 in a range inside that, like
'[2010-01-01 14:10, 2010-01-01 14:30]',15
My expected outcome would be 3 rows like
'[2010-01-01 14:00, 2010-01-01 14:10)',20
'[2010-01-01 14:10, 2010-01-01 14:30)',15
'[2010-01-01 14:30, 2010-01-01 15:00]',20
Is that possible?
Best Answer
You cannot simply use the difference operator
-
, because it does not allow multiple resulting rows:So you need to create three rows and use the intersection operator
*
:Returns:
This applies the operation to all rows in table
tbl
.And it works for all ranges (overlapping completely, in parts or not at all).
Note that my 2nd row matches the upper bound of the input value (unlike your example, which is probably unintended).
All bounds are preserved. Input values trump existing values. This is achieved with (example 1st part):
(
)]
); intersect that with the input range, to get the complementary bound to the lower bound of the input range.To eliminate rows with an
empty
time range:SQL Fiddle.