SQL 查询对我来说太复杂了

标签 sql oracle

我有一个包含 note 列的表,可以有值“Start”或“End”。然后还有其他列,它们可以具有相同的值,但唯一的区别在于“注释”列......

我需要选择将“注释”设置为“开始”的行,但只有那些,没有具有相同值且“注释”设置为“结束”的行。抱歉,解释起来很复杂。我将尝试展示一些示例。

Coll1   Coll2   Coll3   note
-----------------------------
a       a       a       Start
a       a       a       End
b       b       b       Start
b       b       b       End
c       c       c       Start <- I need select those rows
-- There is no row with 'c c c End' combination in the table
d       d       d       Start
d       d       d       End
e       e       e       Start <- I need select those rows
-- There is no row with 'e e e End' combination in the table

有人能帮帮我吗?

最佳答案

尝试使用

SELECT *
  FROM tbl t1
  WHERE t1.note = 'Start' AND NOT EXISTS (SELECT * 
                                            FROM tbl t2 
                                           WHERE t2.note = 'End' 
                                             AND t2.Coll1 = t1.Coll1 
                                             AND t2.Coll2 = t1.Coll1 
                                             AND t2.Coll3 = t1.Coll3)

也许这个查询不是最优的,但是这个查询很容易理解。

关于SQL 查询对我来说太复杂了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40377190/

相关文章:

c# - SQL CLR : Streaming table valued function results

sql - MPTT 表的建议索引

java - Hibernate继承映射: Table per subclass

java - 如何安装 Oracle Java 扩展?

sql - 在数据库中更新/插入/检索重音字符?

sql - tsql函数拆分字符串

mysql - 为什么mysql更喜欢扫描表而不是使用复合索引?

sql - SELECT 哪个 WHERE 子句与结果匹配

sql - 使用花括号和通配符转义 Oracle 文本

c# - ORA-06502、ORA-06512 Oracle 存储过程错误