再发个菜鸟SQL问题...跪求!

来源:百度知道 编辑:UC知道 时间:2024/05/22 18:54:45
有表CaseTray
Ref TrayCode
1 AA
1 BLD
2 EE
2 BLD
3 AA
3 EE
4 EE
4 BLD

要求求成过急出,Ref的TrayCode值有AA或EE的,如果Ref对应的TrayCode 有BLD的就不要..
得出的结果是
Ref TrayCode
3 AA
3 EE
例如,但Ref的值是1的,虽然有AA,但也有BLD,所以不能要,
Ref是2的,虽然有EE,但也有BLD,所以也不能要

需要Ref是同一个值的时候,对应的有AA,或EE,的才要

SELECT * FROM CASETRAY WHERE TRAYCODE Ref TrayCode in('AA',EE') AND NOT EXISTS(SELECT 1 FROM CASETRAY A WHERE REF=CASETRAY.REF AND TRAYCODE='BLD')

正确如下:
Select * from CaseTray
where Ref not in (select Ref from CaseTray where TrayCode='BLD')

select * from CaseTray c
where c.Ref in
((select c.Ref from CaseTray c where c.TrayCode in ('AA','EE'))
minus
(select c.Ref from CaseTray c where c.TrayCode='BLD'))

没看懂什么意思