AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 282435
Accepted
Mike S
Mike S
Asked: 2021-01-02 09:54:03 +0800 CST2021-01-02 09:54:03 +0800 CST 2021-01-02 09:54:03 +0800 CST

如何从两个表中排除其值相互包含的行?

  • 772

我有以下两个表,表 A 和表 B。

我想要做的是返回一个结果集,其中:

  1. 如果表 B 中的开始位置(仅)在表 A 中任何行的开始和结束位置之间,那么我不希望表 B 中的该行出现在结果集中。

  2. 如果表 A 中的任何开始和结束位置位于表 B 中任何行的任何开始和结束位置之间,那么我不希望表 A 中的该行。

这是表A:

RID 起始位置 EndPos
7 45 77
7 118 130
7 197 212
7 218 235

这是表B:

RID 起始位置 EndPos
7 83 87
7 121 132
7 175 179
7 183 191
7 195 214
7 221 237

这是我想要的结果集。您可以看到表 A 中的 197 和 212 位于表 B 中具有 195 和 214 的行之间,因此表 A 中的行不在结果集中。您还可以看到,表 B 中的起始位置 221 位于表 A 中的范围 (218, 235) 之间,因此表 B 中的行也不在结果集中。

RID 起始位置 EndPos
7 45 77
7 83 87
7 118 130
7 175 179
7 183 191
7 195 214
7 218 235

总之,我想排除在哪里:

  • 表 A 的开始位置和结束位置在表 B 的任何开始位置和结束位置之间。
  • 表 B 的开始位置介于表 A 的任何开始位置和结束位置之间。

此外,表中有多个 RID... 我认为(并且仍然认为)我可以使用 FULL 连接(或者可能使用 APPLY)来完成这项工作,但我有点卡住了,这就是为什么我求大家帮忙!

这是生成两个表的 T-SQL:

CREATE TABLE #TableA(
    RID int NULL,
    StartPos int NULL,
    EndPos int NULL
)

INSERT #TableA (RID, StartPos, EndPos) VALUES (7, 45, 77)
INSERT #TableA (RID, StartPos, EndPos) VALUES (7, 118, 130)
INSERT #TableA (RID, StartPos, EndPos) VALUES (7, 197, 212)
INSERT #TableA (RID, StartPos, EndPos) VALUES (7, 218, 235)


CREATE TABLE #TableB(
    RID int NULL,
    StartPos int NULL,
    EndPos int NULL
)

INSERT #TableB (RID, StartPos, EndPos) VALUES (7, 83, 87)
INSERT #TableB (RID, StartPos, EndPos) VALUES (7, 121, 132)
INSERT #TableB (RID, StartPos, EndPos) VALUES (7, 175, 179)
INSERT #TableB (RID, StartPos, EndPos) VALUES (7, 183, 191)
INSERT #TableB (RID, StartPos, EndPos) VALUES (7, 195, 214)
INSERT #TableB (RID, StartPos, EndPos) VALUES (7, 221, 237)


SELECT *
FROM #TableA

SELECT *
FROM #TableB
sql-server t-sql
  • 1 1 个回答
  • 82 Views

1 个回答

  • Voted
  1. Best Answer
    Lennart - Slava Ukraini
    2021-01-02T10:19:04+08:002021-01-02T10:19:04+08:00

    根据你的问题,我认为这就是你想要的:

    select * from #TableB b 
    where not exists (
        select 1 from #TableA a 
        where b.StartPos between a.StartPos and a.EndPos
        and A.RID = b.RID
    )
    union
    select * from #TableA a 
    where not exists (
        select 1 from  #TableB b
        where a.StartPos between b.StartPos and b.EndPos
        and a.EndPos between b.StartPos and b.EndPos
        and A.RID = b.RID
    )
    ;
    

    小提琴

    • 3

相关问题

  • SQL Server - 使用聚集索引时如何存储数据页

  • 我需要为每种类型的查询使用单独的索引,还是一个多列索引可以工作?

  • 什么时候应该使用唯一约束而不是唯一索引?

  • 死锁的主要原因是什么,可以预防吗?

  • 如何确定是否需要或需要索引

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve