我正在尝试编写oracle-sql来从表ABC中选择所有emplides不包括具有三种特定作用的眼睑。示例如下-
表1=ABC
EMPLID nbsp nbsp nbsp;角色
______________________
111 nbsp nbsp nbsp nbsp nbsp nbsp nbsp;苹果
111 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 芒果
111 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 红苹果
222 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 苹果
222 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 橙色
222 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 红色标志
222 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 香蕉
333 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 苹果
333 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 橙色
444 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 苹果
444 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 芒果
444 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 红色标志
555 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 葡萄
666 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 橙色
666 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 葡萄
666 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 蓝莓
表2=详细信息
EMPLID nbsp 姓名 nbsp 电子邮件
__________________________________
111 nbsp nbsp nbsp nbsp nbsp;John nbsp nbsp nbsp info@email.com
222 nbsp nbsp nbsp nbsp nbsp;Erica nbsp nbsp nbsp;info@email.com
等等…
基本上,在上面的例子中,由于苹果、芒果和Red%是三个角色这需要排除在外。sql应该返回的EMPLID和NAME222333555和666。应排除111和444
我尝试创建子选择,但仍然无法工作`在此处输入代码`。任何建议或帮助非常感谢。
使用条件聚合:
SELECT t1.EMPLID,
t1.NAME,
t1.EMAIL
FROM DETAILS t1
INNER JOIN
(
SELECT EMPLID
FROM ABC
GROUP BY EMPLID
HAVING SUM(CASE WHEN ROLE = 'Apple' OR ROLE = 'Mango' OR ROLE LIKE 'Red%'
THEN 1 ELSE 0 END) < 3
) t2
ON t1.EMPLID = t2.EMPLID