我有这个查询,我希望得到所有同时具有ER和(OM,MT或NM)金钱类型的记录,但是这个查询只会带来ER和disergards我的子选择。记录密钥是SSN,PLAN,MONEY_TYPE_CD所以我想要所有具有ER和(OM,MT或NM)的SSN。希望这更清楚。任何帮助表示赞赏。
SELECT A.SSN, A.MONEY_TYPE_CD, A.PLAN_TYPE, A.ER_NUM
FROM FUND_DTL A
WHERE A.MONEY_TYPE_CD = 'ER'
AND A.SSN IN (
SELECT SSN
FROM FUND_DTL
WHERE MONEY_TYPE_CD IN ('OM', 'MT', 'NM')
)
WITH UR;
答案 0 :(得分:0)
WHERE A.MONEY_TYPE_CD = 'ER'
将结果集限制为“ER”代码。
你想要更像这样的东西......
SELECT A.SSN, A.MONEY_TYPE_CD, A.PLAN_TYPE, A.ER_NUM
FROM FUND_DTL A
WHERE A.SSN IN (SELECT SSN
FROM FUND_DTL
WHERE MONEY_TYPE_CD IN ('OM', 'MT', 'NM'))
AND A.SSN IN (SELECT SSN
FROM FUND_DTL
WHERE MONEY_TYPE_CD = 'ER')
WITH UR;
使用INTERSECT的另一个版本
SELECT A.SSN, A.MONEY_TYPE_CD, A.PLAN_TYPE, A.ER_NUM
FROM FUND_DTL A
WHERE A.SSN IN (SELECT SSN
FROM FUND_DTL
WHERE MONEY_TYPE_CD IN ('OM', 'MT', 'NM')
INTERSECT
SELECT SSN
FROM FUND_DTL
WHERE MONEY_TYPE_CD = 'ER')
WITH UR;
两个人都说给我任何记录,其SSN出现在带有'ER'的SSN集和带有('OM','MT','NM')的SSN集合中