Find numbers not included in any range

非 Y 不嫁゛ 提交于 2020-01-05 04:49:01

问题


I understand why the difference operator for ranges fails if the result would be two disjoint ranges. But I'm not sure what the workaround is.

A simple conceptual example would be if I was keeping a calendar and had a record for each meeting, with a timestamp range field storing the time of the meeting; what would be a simple way to generate a list of times that the person was free on a given day?

It seems like a simple, basic thing to do with ranges, but I can't come up with a way to do it that isn't unseemingly complicated.


回答1:


create or replace function range_exclude(anyelement, anyelement) returns anyarray as $$
declare
  r1 text;
  r2 text;
begin
  -- Check input parameters
  if not pg_typeof($1) in ('numrange'::regtype, 'int8range'::regtype, 'daterange'::regtype, 'tstzrange'::regtype) then
    raise exception 'Function accepts only range types but got % type.', pg_typeof($1);
  end if;

  -- If result is single element
  if ($1 &< $2 or $1 &> $2) then
    return array[$1 - $2];
  end if;

  -- Else build array of two intervals
  if lower_inc($1) then r1 := '['; else r1 := '('; end if;
  r1 := r1 || lower($1) || ',' || lower($2);
  if lower_inc($2) then r1 := r1 || ')'; else r1 := r1 || ']'; end if;

  if upper_inc($2) then r2 := '('; else r2 := '['; end if;
  r2 := r2 || upper($2) || ',' || upper($1);
  if upper_inc($1) then r2 := r2 || ']'; else r2 := r2 || ')'; end if;
  return array[r1, r2];
end $$ immutable language plpgsql;

create or replace function range_exclude(anyelement, anyarray) returns anyarray as $$
declare
  i int;
  j int;
begin
  -- Check input parameters
  if not pg_typeof($1) in ('numrange'::regtype, 'int8range'::regtype, 'daterange'::regtype, 'tstzrange'::regtype) then
    raise exception 'Function accepts only range types but got % type.', pg_typeof($1);
  end if;

  if array_length($2,1) is null then
    return array[$1];
  end if;

  $0 := range_exclude($1,$2[array_lower($2,1)]);
  for i in array_lower($2,1) + 1 .. array_upper($2,1) loop
    select array(select x from (select unnest(range_exclude(x,$2[i])) from unnest($0) as t(x)) as t(x) where not isempty(x)) into $0;
  end loop;
  return $0;
end $$ immutable language plpgsql;

select range_exclude(numrange(8,17), array[numrange(10,11), numrange(13,20)]);

Tests:

select range_exclude(numrange(1,10), numrange(5,6));
select range_exclude(numrange(8,17), array[numrange(10,11), numrange(13,15)]);

Results:

{"[1,5)","[6,10)"}
{"[8,10)","[11,13)","[15,17)"}

And same for timestamps:

select range_exclude(
  tstzrange('2016-07-28 8:00','2016-07-28 17:00'), 
  array[
    tstzrange('2016-07-28 10:00','2016-07-28 11:00'),
    tstzrange('2016-07-28 13:00','2016-07-28 15:00')]);

Result:

{"[\"2016-07-28 08:00:00+03\",\"2016-07-28 10:00:00+03\")","[\"2016-07-28 11:00:00+03\",\"2016-07-28 13:00:00+03\")","[\"2016-07-28 15:00:00+03\",\"2016-07-28 17:00:00+03\")"}



回答2:


If the ranges are disjoint use the window function lead() or lag():

create table plan (duration tsrange);
insert into plan values
('[2015-01-01 10:00:00, 2015-01-01 12:00:00)'),
('[2015-01-01 14:00:00, 2015-01-01 16:00:00)'),
('[2015-01-01 18:00:00, 2015-01-01 20:00:00)');

select tsrange(upper(duration), lower(lead(duration) over (order by duration))) free_time
from plan;

                   free_time                   
-----------------------------------------------
 ["2015-01-01 12:00:00","2015-01-01 14:00:00")
 ["2015-01-01 16:00:00","2015-01-01 18:00:00")
 ["2015-01-01 20:00:00",)
(3 rows)    


来源:https://stackoverflow.com/questions/38626369/find-numbers-not-included-in-any-range

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!