gpt4 book ai didi

oracle - 选择数字范围不重叠的地方

转载 作者:行者123 更新时间:2023-12-01 01:44:38 25 4
gpt4 key购买 nike

我有两个表,其中包含有关道路 build 事件的记录:

  • table_a是主列表。
  • table_b是一个遗留列表。

  • 对于每条道路,每年,我想从 table_b 中选择记录。不存在于 table_a .

    另外,记录应该是 不是 沿道路在空间上重叠。更具体地说, from_mto_m table_b 中的记录不应与 from_m 重叠和 to_mtable_a .

    我怎样才能做到这一点? 我没有 Oracle Spatial。

    Excel中的数据(方便查看):

    以下是 Excel 中的数据:

    enter image description here

    中的记录绿色应由查询选择; 中的记录红色不应该。

    DDL:

    表一:
      create table table_a 
    (
    id number(4,0),
    road_id number(4,0),
    year number(4,0),
    from_m number(4,0),
    to_m number(4,0)
    );

    insert into table_a (id,road_id,year,from_m,to_m) values (1,1,2000,0,100);
    insert into table_a (id,road_id,year,from_m,to_m) values (2,1,2005,0,25);
    insert into table_a (id,road_id,year,from_m,to_m) values (3,1,2005,50,75);
    insert into table_a (id,road_id,year,from_m,to_m) values (4,1,2005,75,100);
    insert into table_a (id,road_id,year,from_m,to_m) values (5,1,2010,10,50);
    insert into table_a (id,road_id,year,from_m,to_m) values (6,1,2010,50,90);
    insert into table_a (id,road_id,year,from_m,to_m) values (7,1,2015,40,100);
    insert into table_a (id,road_id,year,from_m,to_m) values (8,2,2020,0,40);
    insert into table_a (id,road_id,year,from_m,to_m) values (9,2,2020,0,40);
    insert into table_a (id,road_id,year,from_m,to_m) values (10,3,2025,90,150);
    commit;

    select * from table_a;

    ID ROAD_ID YEAR FROM_M TO_M
    ---------- ---------- ---------- ---------- ----------
    1 1 2000 0 100
    2 1 2005 0 25
    3 1 2005 50 75
    4 1 2005 75 100
    5 1 2010 10 50
    6 1 2010 50 90
    7 1 2015 40 100
    8 2 2020 0 40
    9 2 2020 0 40
    10 3 2025 90 150

    表 B:
      create table table_b 
    (
    id number(4,0),
    road_id number(4,0),
    year number(4,0),
    from_m number(4,0),
    to_m number(4,0)
    );

    insert into table_b (id,road_id,year,from_m,to_m) values (1,1,1995,0,100);
    insert into table_b (id,road_id,year,from_m,to_m) values (2,1,2001,0,50);
    insert into table_b (id,road_id,year,from_m,to_m) values (3,1,2005,20,80);
    insert into table_b (id,road_id,year,from_m,to_m) values (4,1,2005,0,100);
    insert into table_b (id,road_id,year,from_m,to_m) values (5,1,2010,0,10);
    insert into table_b (id,road_id,year,from_m,to_m) values (6,1,2010,90,100);
    insert into table_b (id,road_id,year,from_m,to_m) values (7,1,2010,5,85);
    insert into table_b (id,road_id,year,from_m,to_m) values (8,1,2015,40,100);
    insert into table_b (id,road_id,year,from_m,to_m) values (9,1,2015,0,40);
    insert into table_b (id,road_id,year,from_m,to_m) values (10,2,2020,0,41);
    insert into table_b (id,road_id,year,from_m,to_m) values (11,3,2025,155,200);
    insert into table_b (id,road_id,year,from_m,to_m) values (12,3,2025,199,300);
    insert into table_b (id,road_id,year,from_m,to_m) values (13,4,2024,5,355);
    commit;

    select * from table_b;

    ID ROAD_ID YEAR FROM_M TO_M
    ---------- ---------- ---------- ---------- ----------
    1 1 1995 0 100
    2 1 2001 0 50
    3 1 2005 20 80
    4 1 2005 0 100
    5 1 2010 0 10
    6 1 2010 90 100
    7 1 2010 5 85
    8 1 2015 40 100
    9 1 2015 0 40
    10 2 2020 0 41
    11 3 2025 155 200
    12 3 2025 199 300
    13 4 2024 5 355

    最佳答案

    NOT EXISTS 子选择可以在这里提供帮助

    SELECT *
    FROM table_b b
    WHERE
    NOT EXISTS (SELECT *
    FROM table_a a
    WHERE
    a.road_id = b.road_id AND
    a.year = b.year AND
    a.to_m > b.from_m AND
    a.from_m < b.to_m)

    让我们看看重叠范围 (f=from, t=to)

    a   -------------------f=======================t-----------------

    b1a -----f=============t-----------------------------------------
    b1b --f=============t--------------------------------------------

    b2a -------------------------------------------f======t----------
    b2b -----------------------------------------------f======t------

    b3 ---------------f=========t-----------------------------------
    b4 ------------------------f===========t------------------------
    b5 ---------------------------------------f===========t---------

    范围 b3、b4 和 b5 重叠。对于所有这些,以下是正确的
    a.to > b.from && a.from < b.to

    对于不重叠的 b1a、b1b 和 b2a、b2b,此条件为假。对于 b1a a.from == b.to , 对于 b1b a.from > b.to因此条件 a.from < b.tofalse .

    对于 b2a a.to == b.from , 对于 b2b a.to < b.from因此条件 a.to > b.fromfalse .

    诀窍是比较 fromto 的一个范围反之亦然。

    见: http://sqlfiddle.com/#!4/85883/3/0

    关于oracle - 选择数字范围不重叠的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51729896/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com