SQL JOIN-仅以一对多关系选择满足所有条件的记录

问题描述

这是一个sybase数据库,遵循t-sql语法(与MSSQL相同/相似)。

我有2张桌子,案子和保险。

CASES              insurance
-----              ---------
casenum            ins_id
                   date_resolved
                   case_num

每个案例编号都有多个关联的保险条目,并且每个保险条目都有一个“日期解析”字段。

我想做的是,仅返回所有相关保险条目都具有date_resolved = null的案例编号。
我得到的是个案编号和相关的保险条目为空。

例如,如果一个案例编号有3个保险条目,其中2个具有无效的date_resolved,但有1个确实具有date_resolved的值,我的查询将返回该案例编号,因为这2个条目为空。

这是我尝试过的:

select casenum
from cases
left join (select date_resolved from insurance where date_resolved is null) ins
on ins.case_num = cases.casenum

select casenum
from cases
inner join insurance on cases.casenum = insurance.case_num
where insurance.date_resolved is null

很抱歉,如果这是初学者的错误,我已经搜寻了董事会,发现了许多类似的问题,但似乎没有一个能解决我的问题。

编辑- 使用下面提供的答案之一,我将其合并到我的查询中,最终得到了这样的内容(我认为这是可行的):

select casenum,*other stuff*
from cases
inner join (select i.case_num from insurance i group by i.case_num having max(date_resolved) is null) ins
on ins.case_num = cases.casenum
where *other stuff from cases table*
and *more stuff from cases table*

我真的很感激!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)